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 #include "VectorImage.h"
8 #include "AutoRestoreSVGState.h"
10 #include "gfxContext.h"
11 #include "gfxDrawable.h"
12 #include "gfxPlatform.h"
15 #include "mozilla/MemoryReporting.h"
16 #include "mozilla/MediaFeatureChange.h"
17 #include "mozilla/dom/Event.h"
18 #include "mozilla/dom/SVGSVGElement.h"
19 #include "mozilla/dom/SVGDocument.h"
20 #include "mozilla/gfx/2D.h"
21 #include "mozilla/PresShell.h"
22 #include "mozilla/ProfilerLabels.h"
23 #include "mozilla/RefPtr.h"
24 #include "mozilla/StaticPrefs_image.h"
25 #include "mozilla/SVGObserverUtils.h" // for SVGRenderingObserver
26 #include "mozilla/SVGUtils.h"
28 #include "nsIStreamListener.h"
29 #include "nsMimeTypes.h"
30 #include "nsPresContext.h"
33 #include "nsStubDocumentObserver.h"
34 #include "nsWindowSizes.h"
35 #include "ImageRegion.h"
36 #include "ISurfaceProvider.h"
37 #include "LookupResult.h"
38 #include "Orientation.h"
39 #include "SVGDocumentWrapper.h"
40 #include "SVGDrawingCallback.h"
41 #include "SVGDrawingParameters.h"
42 #include "nsIDOMEventListener.h"
43 #include "SurfaceCache.h"
44 #include "BlobSurfaceProvider.h"
45 #include "mozilla/dom/Document.h"
46 #include "mozilla/dom/DocumentInlines.h"
47 #include "mozilla/image/Resolution.h"
48 #include "WindowRenderer.h"
53 using namespace dom::SVGPreserveAspectRatio_Binding
;
55 using namespace layers
;
59 // Helper-class: SVGRootRenderingObserver
60 class SVGRootRenderingObserver final
: public SVGRenderingObserver
{
64 SVGRootRenderingObserver(SVGDocumentWrapper
* aDocWrapper
,
65 VectorImage
* aVectorImage
)
66 : mDocWrapper(aDocWrapper
),
67 mVectorImage(aVectorImage
),
68 mHonoringInvalidations(true) {
69 MOZ_ASSERT(mDocWrapper
, "Need a non-null SVG document wrapper");
70 MOZ_ASSERT(mVectorImage
, "Need a non-null VectorImage");
73 Element
* elem
= GetReferencedElementWithoutObserving();
74 MOZ_ASSERT(elem
, "no root SVG node for us to observe");
76 SVGObserverUtils::AddRenderingObserver(elem
, this);
77 mInObserverSet
= true;
80 void ResumeHonoringInvalidations() { mHonoringInvalidations
= true; }
83 virtual ~SVGRootRenderingObserver() {
84 // This needs to call our GetReferencedElementWithoutObserving override,
85 // so must be called here rather than in our base class's dtor.
89 Element
* GetReferencedElementWithoutObserving() final
{
90 return mDocWrapper
->GetRootSVGElem();
93 virtual void OnRenderingChange() override
{
94 Element
* elem
= GetReferencedElementWithoutObserving();
95 MOZ_ASSERT(elem
, "missing root SVG node");
97 if (mHonoringInvalidations
&& !mDocWrapper
->ShouldIgnoreInvalidation()) {
98 nsIFrame
* frame
= elem
->GetPrimaryFrame();
99 if (!frame
|| frame
->PresShell()->IsDestroying()) {
100 // We're being destroyed. Bail out.
104 // Ignore further invalidations until we draw.
105 mHonoringInvalidations
= false;
107 mVectorImage
->InvalidateObserversOnNextRefreshDriverTick();
110 // Our caller might've removed us from rendering-observer list.
111 // Add ourselves back!
112 if (!mInObserverSet
) {
113 SVGObserverUtils::AddRenderingObserver(elem
, this);
114 mInObserverSet
= true;
119 const RefPtr
<SVGDocumentWrapper
> mDocWrapper
;
120 VectorImage
* const mVectorImage
; // Raw pointer because it owns me.
121 bool mHonoringInvalidations
;
124 NS_IMPL_ISUPPORTS(SVGRootRenderingObserver
, nsIMutationObserver
)
126 class SVGParseCompleteListener final
: public nsStubDocumentObserver
{
130 SVGParseCompleteListener(SVGDocument
* aDocument
, VectorImage
* aImage
)
131 : mDocument(aDocument
), mImage(aImage
) {
132 MOZ_ASSERT(mDocument
, "Need an SVG document");
133 MOZ_ASSERT(mImage
, "Need an image");
135 mDocument
->AddObserver(this);
139 ~SVGParseCompleteListener() {
141 // The document must have been destroyed before we got our event.
142 // Otherwise this can't happen, since documents hold strong references to
149 void EndLoad(Document
* aDocument
) override
{
150 MOZ_ASSERT(aDocument
== mDocument
, "Got EndLoad for wrong document?");
152 // OnSVGDocumentParsed will release our owner's reference to us, so ensure
153 // we stick around long enough to complete our work.
154 RefPtr
<SVGParseCompleteListener
> kungFuDeathGrip(this);
156 mImage
->OnSVGDocumentParsed();
160 MOZ_ASSERT(mDocument
, "Duplicate call to Cancel");
162 mDocument
->RemoveObserver(this);
168 RefPtr
<SVGDocument
> mDocument
;
169 VectorImage
* const mImage
; // Raw pointer to owner.
172 NS_IMPL_ISUPPORTS(SVGParseCompleteListener
, nsIDocumentObserver
)
174 class SVGLoadEventListener final
: public nsIDOMEventListener
{
178 SVGLoadEventListener(Document
* aDocument
, VectorImage
* aImage
)
179 : mDocument(aDocument
), mImage(aImage
) {
180 MOZ_ASSERT(mDocument
, "Need an SVG document");
181 MOZ_ASSERT(mImage
, "Need an image");
183 mDocument
->AddEventListener(u
"MozSVGAsImageDocumentLoad"_ns
, this, true,
188 ~SVGLoadEventListener() {
190 // The document must have been destroyed before we got our event.
191 // Otherwise this can't happen, since documents hold strong references to
198 NS_IMETHOD
HandleEvent(Event
* aEvent
) override
{
199 MOZ_ASSERT(mDocument
, "Need an SVG document. Received multiple events?");
201 // OnSVGDocumentLoaded will release our owner's reference
202 // to us, so ensure we stick around long enough to complete our work.
203 RefPtr
<SVGLoadEventListener
> kungFuDeathGrip(this);
206 nsAutoString eventType
;
207 aEvent
->GetType(eventType
);
208 MOZ_ASSERT(eventType
.EqualsLiteral("MozSVGAsImageDocumentLoad"),
209 "Received unexpected event");
212 mImage
->OnSVGDocumentLoaded();
218 MOZ_ASSERT(mDocument
, "Duplicate call to Cancel");
220 mDocument
->RemoveEventListener(u
"MozSVGAsImageDocumentLoad"_ns
, this,
227 nsCOMPtr
<Document
> mDocument
;
228 VectorImage
* const mImage
; // Raw pointer to owner.
231 NS_IMPL_ISUPPORTS(SVGLoadEventListener
, nsIDOMEventListener
)
233 SVGDrawingCallback::SVGDrawingCallback(SVGDocumentWrapper
* aSVGDocumentWrapper
,
234 const IntSize
& aViewportSize
,
235 const IntSize
& aSize
,
236 uint32_t aImageFlags
)
237 : mSVGDocumentWrapper(aSVGDocumentWrapper
),
238 mViewportSize(aViewportSize
),
240 mImageFlags(aImageFlags
) {}
242 SVGDrawingCallback::~SVGDrawingCallback() = default;
244 // Based loosely on SVGIntegrationUtils' PaintFrameCallback::operator()
245 bool SVGDrawingCallback::operator()(gfxContext
* aContext
,
246 const gfxRect
& aFillRect
,
247 const SamplingFilter aSamplingFilter
,
248 const gfxMatrix
& aTransform
) {
249 MOZ_ASSERT(mSVGDocumentWrapper
, "need an SVGDocumentWrapper");
251 // Get (& sanity-check) the helper-doc's presShell
252 RefPtr
<PresShell
> presShell
= mSVGDocumentWrapper
->GetPresShell();
253 MOZ_ASSERT(presShell
, "GetPresShell returned null for an SVG image?");
255 Document
* doc
= presShell
->GetDocument();
256 [[maybe_unused
]] nsIURI
* uri
= doc
? doc
->GetDocumentURI() : nullptr;
257 AUTO_PROFILER_LABEL_DYNAMIC_NSCSTRING(
258 "SVG Image drawing", GRAPHICS
,
259 nsPrintfCString("%dx%d %s", mSize
.width
, mSize
.height
,
260 uri
? uri
->GetSpecOrDefault().get() : "N/A"));
262 gfxContextAutoSaveRestore
contextRestorer(aContext
);
264 // Clip to aFillRect so that we don't paint outside.
265 aContext
->Clip(aFillRect
);
267 gfxMatrix matrix
= aTransform
;
268 if (!matrix
.Invert()) {
271 aContext
->SetMatrixDouble(
272 aContext
->CurrentMatrixDouble().PreMultiply(matrix
).PreScale(
273 double(mSize
.width
) / mViewportSize
.width
,
274 double(mSize
.height
) / mViewportSize
.height
));
276 nsPresContext
* presContext
= presShell
->GetPresContext();
277 MOZ_ASSERT(presContext
, "pres shell w/out pres context");
279 nsRect
svgRect(0, 0, presContext
->DevPixelsToAppUnits(mViewportSize
.width
),
280 presContext
->DevPixelsToAppUnits(mViewportSize
.height
));
282 RenderDocumentFlags renderDocFlags
=
283 RenderDocumentFlags::IgnoreViewportScrolling
;
284 if (!(mImageFlags
& imgIContainer::FLAG_SYNC_DECODE
)) {
285 renderDocFlags
|= RenderDocumentFlags::AsyncDecodeImages
;
287 if (mImageFlags
& imgIContainer::FLAG_HIGH_QUALITY_SCALING
) {
288 renderDocFlags
|= RenderDocumentFlags::UseHighQualityScaling
;
291 presShell
->RenderDocument(svgRect
, renderDocFlags
,
292 NS_RGBA(0, 0, 0, 0), // transparent
298 // Implement VectorImage's nsISupports-inherited methods
299 NS_IMPL_ISUPPORTS(VectorImage
, imgIContainer
, nsIStreamListener
,
302 //------------------------------------------------------------------------------
303 // Constructor / Destructor
305 VectorImage::VectorImage(nsIURI
* aURI
/* = nullptr */)
306 : ImageResource(aURI
), // invoke superclass's constructor
308 mIsInitialized(false),
310 mIsFullyLoaded(false),
311 mHaveAnimations(false),
312 mHasPendingInvalidation(false) {}
314 VectorImage::~VectorImage() {
315 ReportDocumentUseCounters();
316 CancelAllListeners();
317 SurfaceCache::RemoveImage(ImageKey(this));
320 //------------------------------------------------------------------------------
321 // Methods inherited from Image.h
323 nsresult
VectorImage::Init(const char* aMimeType
, uint32_t aFlags
) {
324 // We don't support re-initialization
325 if (mIsInitialized
) {
326 return NS_ERROR_ILLEGAL_VALUE
;
329 MOZ_ASSERT(!mIsFullyLoaded
&& !mHaveAnimations
&& !mError
,
330 "Flags unexpectedly set before initialization");
331 MOZ_ASSERT(!strcmp(aMimeType
, IMAGE_SVG_XML
), "Unexpected mimetype");
333 mDiscardable
= !!(aFlags
& INIT_FLAG_DISCARDABLE
);
335 // Lock this image's surfaces in the SurfaceCache if we're not discardable.
338 SurfaceCache::LockImage(ImageKey(this));
341 mIsInitialized
= true;
345 size_t VectorImage::SizeOfSourceWithComputedFallback(
346 SizeOfState
& aState
) const {
347 if (!mSVGDocumentWrapper
) {
348 return 0; // No document, so no memory used for the document.
351 SVGDocument
* doc
= mSVGDocumentWrapper
->GetDocument();
353 return 0; // No document, so no memory used for the document.
356 nsWindowSizes
windowSizes(aState
);
357 doc
->DocAddSizeOfIncludingThis(windowSizes
);
359 if (windowSizes
.getTotalSize() == 0) {
360 // MallocSizeOf fails on this platform. Because we also use this method for
361 // determining the size of cache entries, we need to return something
362 // reasonable here. Unfortunately, there's no way to estimate the document's
363 // size accurately, so we just use a constant value of 100KB, which will
364 // generally underestimate the true size.
368 return windowSizes
.getTotalSize();
371 nsresult
VectorImage::OnImageDataComplete(nsIRequest
* aRequest
,
372 nsresult aStatus
, bool aLastPart
) {
373 // Call our internal OnStopRequest method, which only talks to our embedded
374 // SVG document. This won't have any effect on our ProgressTracker.
375 nsresult finalStatus
= OnStopRequest(aRequest
, aStatus
);
377 // Give precedence to Necko failure codes.
378 if (NS_FAILED(aStatus
)) {
379 finalStatus
= aStatus
;
382 Progress loadProgress
= LoadCompleteProgress(aLastPart
, mError
, finalStatus
);
384 if (mIsFullyLoaded
|| mError
) {
385 // Our document is loaded, so we're ready to notify now.
386 mProgressTracker
->SyncNotifyProgress(loadProgress
);
388 // Record our progress so far; we'll actually send the notifications in
389 // OnSVGDocumentLoaded or OnSVGDocumentError.
390 mLoadProgress
= Some(loadProgress
);
396 nsresult
VectorImage::OnImageDataAvailable(nsIRequest
* aRequest
,
397 nsIInputStream
* aInStr
,
398 uint64_t aSourceOffset
,
400 return OnDataAvailable(aRequest
, aInStr
, aSourceOffset
, aCount
);
403 nsresult
VectorImage::StartAnimation() {
405 return NS_ERROR_FAILURE
;
408 MOZ_ASSERT(ShouldAnimate(), "Should not animate!");
410 mSVGDocumentWrapper
->StartAnimation();
414 nsresult
VectorImage::StopAnimation() {
417 rv
= NS_ERROR_FAILURE
;
419 MOZ_ASSERT(mIsFullyLoaded
&& mHaveAnimations
,
420 "Should not have been animating!");
422 mSVGDocumentWrapper
->StopAnimation();
429 bool VectorImage::ShouldAnimate() {
430 return ImageResource::ShouldAnimate() && mIsFullyLoaded
&& mHaveAnimations
;
434 VectorImage::SetAnimationStartTime(const TimeStamp
& aTime
) {
435 // We don't care about animation start time.
438 //------------------------------------------------------------------------------
439 // imgIContainer methods
441 //******************************************************************************
443 VectorImage::GetWidth(int32_t* aWidth
) {
444 if (mError
|| !mIsFullyLoaded
) {
445 // XXXdholbert Technically we should leave outparam untouched when we
446 // fail. But since many callers don't check for failure, we set it to 0 on
447 // failure, for sane/predictable results.
449 return NS_ERROR_FAILURE
;
452 SVGSVGElement
* rootElem
= mSVGDocumentWrapper
->GetRootSVGElem();
453 if (MOZ_UNLIKELY(!rootElem
)) {
454 // Unlikely to reach this code; we should have a root SVG elem, since we
455 // finished loading without errors. But we can sometimes get here during
456 // shutdown (as part of gathering a memory report) if the internal SVG
457 // document has already been torn down by a shutdown listener.
459 return NS_ERROR_FAILURE
;
461 LengthPercentage rootElemWidth
= rootElem
->GetIntrinsicWidth();
463 if (!rootElemWidth
.IsLength()) {
465 return NS_ERROR_FAILURE
;
468 *aWidth
= SVGUtils::ClampToInt(rootElemWidth
.AsLength().ToCSSPixels());
472 //******************************************************************************
473 nsresult
VectorImage::GetNativeSizes(nsTArray
<IntSize
>& aNativeSizes
) {
474 return NS_ERROR_NOT_IMPLEMENTED
;
477 //******************************************************************************
478 size_t VectorImage::GetNativeSizesLength() { return 0; }
480 //******************************************************************************
482 VectorImage::RequestRefresh(const TimeStamp
& aTime
) {
483 if (HadRecentRefresh(aTime
)) {
487 Document
* doc
= mSVGDocumentWrapper
->GetDocument();
489 // We are racing between shutdown and a refresh.
495 mSVGDocumentWrapper
->TickRefreshDriver();
497 if (mHasPendingInvalidation
) {
498 SendInvalidationNotifications();
502 void VectorImage::SendInvalidationNotifications() {
503 // Animated images don't send out invalidation notifications as soon as
504 // they're generated. Instead, InvalidateObserversOnNextRefreshDriverTick
505 // records that there are pending invalidations and then returns immediately.
506 // The notifications are actually sent from RequestRefresh(). We send these
507 // notifications there to ensure that there is actually a document observing
508 // us. Otherwise, the notifications are just wasted effort.
510 // Non-animated images post an event to call this method from
511 // InvalidateObserversOnNextRefreshDriverTick, because RequestRefresh is never
512 // called for them. Ordinarily this isn't needed, since we send out
513 // invalidation notifications in OnSVGDocumentLoaded, but in rare cases the
514 // SVG document may not be 100% ready to render at that time. In those cases
515 // we would miss the subsequent invalidations if we didn't send out the
516 // notifications indirectly in |InvalidateObservers...|.
518 mHasPendingInvalidation
= false;
520 if (SurfaceCache::InvalidateImage(ImageKey(this))) {
521 // If we had any surface providers in the cache, make sure we handle future
523 MOZ_ASSERT(mRenderingObserver
, "Should have a rendering observer by now");
524 mRenderingObserver
->ResumeHonoringInvalidations();
527 if (mProgressTracker
) {
528 mProgressTracker
->SyncNotifyProgress(FLAG_FRAME_COMPLETE
,
529 GetMaxSizedIntRect());
533 NS_IMETHODIMP_(IntRect
)
534 VectorImage::GetImageSpaceInvalidationRect(const IntRect
& aRect
) {
538 //******************************************************************************
540 VectorImage::GetHeight(int32_t* aHeight
) {
541 if (mError
|| !mIsFullyLoaded
) {
542 // XXXdholbert Technically we should leave outparam untouched when we
543 // fail. But since many callers don't check for failure, we set it to 0 on
544 // failure, for sane/predictable results.
546 return NS_ERROR_FAILURE
;
549 SVGSVGElement
* rootElem
= mSVGDocumentWrapper
->GetRootSVGElem();
550 if (MOZ_UNLIKELY(!rootElem
)) {
551 // Unlikely to reach this code; we should have a root SVG elem, since we
552 // finished loading without errors. But we can sometimes get here during
553 // shutdown (as part of gathering a memory report) if the internal SVG
554 // document has already been torn down by a shutdown listener.
556 return NS_ERROR_FAILURE
;
558 LengthPercentage rootElemHeight
= rootElem
->GetIntrinsicHeight();
560 if (!rootElemHeight
.IsLength()) {
562 return NS_ERROR_FAILURE
;
565 *aHeight
= SVGUtils::ClampToInt(rootElemHeight
.AsLength().ToCSSPixels());
569 //******************************************************************************
571 VectorImage::GetIntrinsicSize(nsSize
* aSize
) {
572 if (mError
|| !mIsFullyLoaded
) {
573 return NS_ERROR_FAILURE
;
576 nsIFrame
* rootFrame
= mSVGDocumentWrapper
->GetRootLayoutFrame();
578 return NS_ERROR_FAILURE
;
581 *aSize
= nsSize(-1, -1);
582 IntrinsicSize rfSize
= rootFrame
->GetIntrinsicSize();
584 aSize
->width
= *rfSize
.width
;
587 aSize
->height
= *rfSize
.height
;
592 //******************************************************************************
593 AspectRatio
VectorImage::GetIntrinsicRatio() {
594 if (mError
|| !mIsFullyLoaded
) {
597 nsIFrame
* rootFrame
= mSVGDocumentWrapper
->GetRootLayoutFrame();
601 return rootFrame
->GetIntrinsicRatio();
604 NS_IMETHODIMP_(Orientation
)
605 VectorImage::GetOrientation() { return Orientation(); }
607 NS_IMETHODIMP_(Resolution
)
608 VectorImage::GetResolution() { return {}; }
610 //******************************************************************************
612 VectorImage::GetType(uint16_t* aType
) {
613 NS_ENSURE_ARG_POINTER(aType
);
615 *aType
= imgIContainer::TYPE_VECTOR
;
619 //******************************************************************************
621 VectorImage::GetProviderId(uint32_t* aId
) {
622 NS_ENSURE_ARG_POINTER(aId
);
624 *aId
= ImageResource::GetImageProviderId();
628 //******************************************************************************
630 VectorImage::GetAnimated(bool* aAnimated
) {
631 if (mError
|| !mIsFullyLoaded
) {
632 return NS_ERROR_FAILURE
;
635 *aAnimated
= mSVGDocumentWrapper
->IsAnimated();
639 //******************************************************************************
640 int32_t VectorImage::GetFirstFrameDelay() {
645 if (!mSVGDocumentWrapper
->IsAnimated()) {
649 // We don't really have a frame delay, so just pretend that we constantly
655 VectorImage::WillDrawOpaqueNow() {
656 return false; // In general, SVG content is not opaque.
659 //******************************************************************************
660 NS_IMETHODIMP_(already_AddRefed
<SourceSurface
>)
661 VectorImage::GetFrame(uint32_t aWhichFrame
, uint32_t aFlags
) {
666 // Look up height & width
667 // ----------------------
668 SVGSVGElement
* svgElem
= mSVGDocumentWrapper
->GetRootSVGElem();
670 "Should have a root SVG elem, since we finished "
671 "loading without errors");
672 LengthPercentage width
= svgElem
->GetIntrinsicWidth();
673 LengthPercentage height
= svgElem
->GetIntrinsicHeight();
674 if (!width
.IsLength() || !height
.IsLength()) {
675 // The SVG is lacking a definite size for its width or height, so we do not
676 // know how big of a surface to generate. Hence, we just bail.
680 nsIntSize
imageIntSize(SVGUtils::ClampToInt(width
.AsLength().ToCSSPixels()),
681 SVGUtils::ClampToInt(height
.AsLength().ToCSSPixels()));
683 return GetFrameAtSize(imageIntSize
, aWhichFrame
, aFlags
);
686 NS_IMETHODIMP_(already_AddRefed
<SourceSurface
>)
687 VectorImage::GetFrameAtSize(const IntSize
& aSize
, uint32_t aWhichFrame
,
689 MOZ_ASSERT(aWhichFrame
<= FRAME_MAX_VALUE
);
691 AutoProfilerImagePaintMarker
PROFILER_RAII(this);
693 NotifyDrawingObservers();
696 if (aSize
.IsEmpty() || aWhichFrame
> FRAME_MAX_VALUE
|| mError
||
701 uint32_t whichFrame
= mHaveAnimations
? aWhichFrame
: FRAME_FIRST
;
703 auto [sourceSurface
, decodeSize
] =
704 LookupCachedSurface(aSize
, SVGImageContext(), aFlags
);
706 return sourceSurface
.forget();
709 if (mSVGDocumentWrapper
->IsDrawing()) {
710 NS_WARNING("Refusing to make re-entrant call to VectorImage::Draw");
714 float animTime
= (whichFrame
== FRAME_FIRST
)
716 : mSVGDocumentWrapper
->GetCurrentTimeAsFloat();
718 // By using a null gfxContext, we ensure that we will always attempt to
719 // create a surface, even if we aren't capable of caching it (e.g. due to our
720 // flags, having an animation, etc). Otherwise CreateSurface will assume that
721 // the caller is capable of drawing directly to its own draw target if we
723 SVGImageContext svgContext
;
724 SVGDrawingParameters
params(
725 nullptr, decodeSize
, aSize
, ImageRegion::Create(decodeSize
),
726 SamplingFilter::POINT
, svgContext
, animTime
, aFlags
, 1.0);
728 bool didCache
; // Was the surface put into the cache?
730 AutoRestoreSVGState
autoRestore(params
, mSVGDocumentWrapper
,
731 /* aContextPaint */ false);
733 RefPtr
<gfxDrawable
> svgDrawable
= CreateSVGDrawable(params
);
734 RefPtr
<SourceSurface
> surface
= CreateSurface(params
, svgDrawable
, didCache
);
736 MOZ_ASSERT(!didCache
);
740 SendFrameComplete(didCache
, params
.flags
);
741 return surface
.forget();
745 VectorImage::IsImageContainerAvailable(WindowRenderer
* aRenderer
,
747 if (mError
|| !mIsFullyLoaded
||
748 aRenderer
->GetBackendType() != LayersBackend::LAYERS_WR
) {
752 if (mHaveAnimations
&& !StaticPrefs::image_svg_blob_image()) {
753 // We don't support rasterizing animation SVGs. We can put them in a blob
754 // recording however instead of using fallback.
761 //******************************************************************************
762 NS_IMETHODIMP_(ImgDrawResult
)
763 VectorImage::GetImageProvider(WindowRenderer
* aRenderer
,
764 const gfx::IntSize
& aSize
,
765 const SVGImageContext
& aSVGContext
,
766 const Maybe
<ImageIntRegion
>& aRegion
,
768 WebRenderImageProvider
** aProvider
) {
769 MOZ_ASSERT(NS_IsMainThread());
770 MOZ_ASSERT(aRenderer
);
771 MOZ_ASSERT(!(aFlags
& FLAG_BYPASS_SURFACE_CACHE
), "Unsupported flags");
773 // We don't need to check if the size is too big since we only support
774 // WebRender backends.
775 if (aSize
.IsEmpty()) {
776 return ImgDrawResult::BAD_ARGS
;
780 return ImgDrawResult::BAD_IMAGE
;
783 if (!mIsFullyLoaded
) {
784 return ImgDrawResult::NOT_READY
;
787 if (mHaveAnimations
&& !(aFlags
& FLAG_RECORD_BLOB
)) {
788 // We don't support rasterizing animation SVGs. We can put them in a blob
789 // recording however instead of using fallback.
790 return ImgDrawResult::NOT_SUPPORTED
;
793 AutoProfilerImagePaintMarker
PROFILER_RAII(this);
795 NotifyDrawingObservers();
798 // Only blob recordings support a region to restrict drawing.
799 const bool blobRecording
= aFlags
& FLAG_RECORD_BLOB
;
800 MOZ_ASSERT_IF(!blobRecording
, aRegion
.isNothing());
802 LookupResult
result(MatchType::NOT_FOUND
);
804 mHaveAnimations
? PlaybackType::eAnimated
: PlaybackType::eStatic
;
805 auto surfaceFlags
= ToSurfaceFlags(aFlags
);
807 SVGImageContext newSVGContext
= aSVGContext
;
808 bool contextPaint
= MaybeRestrictSVGContext(newSVGContext
, aFlags
);
810 SurfaceKey surfaceKey
= VectorSurfaceKey(aSize
, aRegion
, newSVGContext
,
811 surfaceFlags
, playbackType
);
812 if ((aFlags
& FLAG_SYNC_DECODE
) || !(aFlags
& FLAG_HIGH_QUALITY_SCALING
)) {
813 result
= SurfaceCache::Lookup(ImageKey(this), surfaceKey
,
814 /* aMarkUsed = */ true);
816 result
= SurfaceCache::LookupBestMatch(ImageKey(this), surfaceKey
,
817 /* aMarkUsed = */ true);
820 // Unless we get a best match (exact or factor of 2 limited), then we want to
821 // generate a new recording/rerasterize, even if we have a substitute.
822 if (result
&& (result
.Type() == MatchType::EXACT
||
823 result
.Type() == MatchType::SUBSTITUTE_BECAUSE_BEST
)) {
824 result
.Surface().TakeProvider(aProvider
);
825 return ImgDrawResult::SUCCESS
;
828 // Ensure we store the surface with the correct key if we switched to factor
829 // of 2 sizing or we otherwise got clamped.
830 IntSize
rasterSize(aSize
);
831 if (!result
.SuggestedSize().IsEmpty()) {
832 rasterSize
= result
.SuggestedSize();
833 surfaceKey
= surfaceKey
.CloneWithSize(rasterSize
);
836 // We're about to rerasterize, which may mean that some of the previous
837 // surfaces we've rasterized aren't useful anymore. We can allow them to
838 // expire from the cache by unlocking them here, and then sending out an
839 // invalidation. If this image is locked, any surfaces that are still useful
840 // will become locked again when Draw touches them, and the remainder will
841 // eventually expire.
842 bool mayCache
= SurfaceCache::CanHold(rasterSize
);
844 SurfaceCache::UnlockEntries(ImageKey(this));
847 // Blob recorded vector images just create a provider responsible for
848 // generating blob keys and recording bindings. The recording won't happen
849 // until the caller requests the key explicitly.
850 RefPtr
<ISurfaceProvider
> provider
;
852 provider
= MakeRefPtr
<BlobSurfaceProvider
>(ImageKey(this), surfaceKey
,
853 mSVGDocumentWrapper
, aFlags
);
855 if (mSVGDocumentWrapper
->IsDrawing()) {
856 NS_WARNING("Refusing to make re-entrant call to VectorImage::Draw");
857 return ImgDrawResult::TEMPORARY_ERROR
;
860 if (!SurfaceCache::IsLegalSize(rasterSize
) ||
861 !Factory::AllowedSurfaceSize(rasterSize
)) {
862 // If either of these is true then the InitWithDrawable call below will
863 // fail, so fail early and use this opportunity to return NOT_SUPPORTED
864 // instead of TEMPORARY_ERROR as we do for any InitWithDrawable failure.
865 // This means that we will use fallback which has a path that will draw
866 // directly into the gfxContext without having to allocate a surface. It
867 // means we will have to use fallback and re-rasterize for everytime we
868 // have to draw this image, but it's better than not drawing anything at
870 return ImgDrawResult::NOT_SUPPORTED
;
873 // We aren't using blobs, so we need to rasterize.
875 mHaveAnimations
? mSVGDocumentWrapper
->GetCurrentTimeAsFloat() : 0.0f
;
877 // By using a null gfxContext, we ensure that we will always attempt to
878 // create a surface, even if we aren't capable of caching it (e.g. due to
879 // our flags, having an animation, etc). Otherwise CreateSurface will assume
880 // that the caller is capable of drawing directly to its own draw target if
882 SVGDrawingParameters
params(
883 nullptr, rasterSize
, aSize
, ImageRegion::Create(rasterSize
),
884 SamplingFilter::POINT
, newSVGContext
, animTime
, aFlags
, 1.0);
886 RefPtr
<gfxDrawable
> svgDrawable
= CreateSVGDrawable(params
);
887 AutoRestoreSVGState
autoRestore(params
, mSVGDocumentWrapper
, contextPaint
);
889 mSVGDocumentWrapper
->UpdateViewportBounds(params
.viewportSize
);
890 mSVGDocumentWrapper
->FlushImageTransformInvalidation();
892 // Given we have no context, the default backend is fine.
893 BackendType backend
=
894 gfxPlatform::GetPlatform()->GetDefaultContentBackend();
896 // Try to create an imgFrame, initializing the surface it contains by
897 // drawing our gfxDrawable into it. (We use FILTER_NEAREST since we never
899 auto frame
= MakeNotNull
<RefPtr
<imgFrame
>>();
900 nsresult rv
= frame
->InitWithDrawable(
901 svgDrawable
, params
.size
, SurfaceFormat::OS_RGBA
, SamplingFilter::POINT
,
902 params
.flags
, backend
);
904 // If we couldn't create the frame, it was probably because it would end
905 // up way too big. Generally it also wouldn't fit in the cache, but the
906 // prefs could be set such that the cache isn't the limiting factor.
908 return ImgDrawResult::TEMPORARY_ERROR
;
912 MakeRefPtr
<SimpleSurfaceProvider
>(ImageKey(this), surfaceKey
, frame
);
916 // Attempt to cache the frame.
917 if (SurfaceCache::Insert(WrapNotNull(provider
)) == InsertOutcome::SUCCESS
) {
918 if (rasterSize
!= aSize
) {
919 // We created a new surface that wasn't the size we requested, which
920 // means we entered factor-of-2 mode. We should purge any surfaces we
921 // no longer need rather than waiting for the cache to expire them.
922 SurfaceCache::PruneImage(ImageKey(this));
925 SendFrameComplete(/* aDidCache */ true, aFlags
);
929 MOZ_ASSERT(provider
);
930 provider
.forget(aProvider
);
931 return ImgDrawResult::SUCCESS
;
934 bool VectorImage::MaybeRestrictSVGContext(SVGImageContext
& aSVGContext
,
936 bool overridePAR
= (aFlags
& FLAG_FORCE_PRESERVEASPECTRATIO_NONE
);
938 bool haveContextPaint
= aSVGContext
.GetContextPaint();
939 bool blockContextPaint
=
940 haveContextPaint
&& !SVGContextPaint::IsAllowedForImageFromURI(mURI
);
942 if (overridePAR
|| blockContextPaint
) {
944 // The SVGImageContext must take account of the preserveAspectRatio
946 MOZ_ASSERT(!aSVGContext
.GetPreserveAspectRatio(),
947 "FLAG_FORCE_PRESERVEASPECTRATIO_NONE is not expected if a "
948 "preserveAspectRatio override is supplied");
949 Maybe
<SVGPreserveAspectRatio
> aspectRatio
= Some(SVGPreserveAspectRatio(
950 SVG_PRESERVEASPECTRATIO_NONE
, SVG_MEETORSLICE_UNKNOWN
));
951 aSVGContext
.SetPreserveAspectRatio(aspectRatio
);
954 if (blockContextPaint
) {
955 // The SVGImageContext must not include context paint if the image is
956 // not allowed to use it:
957 aSVGContext
.ClearContextPaint();
961 return haveContextPaint
&& !blockContextPaint
;
964 //******************************************************************************
965 NS_IMETHODIMP_(ImgDrawResult
)
966 VectorImage::Draw(gfxContext
* aContext
, const nsIntSize
& aSize
,
967 const ImageRegion
& aRegion
, uint32_t aWhichFrame
,
968 SamplingFilter aSamplingFilter
,
969 const SVGImageContext
& aSVGContext
, uint32_t aFlags
,
971 if (aWhichFrame
> FRAME_MAX_VALUE
) {
972 return ImgDrawResult::BAD_ARGS
;
976 return ImgDrawResult::BAD_ARGS
;
980 return ImgDrawResult::BAD_IMAGE
;
983 if (!mIsFullyLoaded
) {
984 return ImgDrawResult::NOT_READY
;
987 if (mAnimationConsumers
== 0 && mHaveAnimations
) {
988 SendOnUnlockedDraw(aFlags
);
991 // We should bypass the cache when:
992 // - We are using a DrawTargetRecording because we prefer the drawing commands
993 // in general to the rasterized surface. This allows blob images to avoid
994 // rasterized SVGs with WebRender.
995 if (aContext
->GetDrawTarget()->GetBackendType() == BackendType::RECORDING
) {
996 aFlags
|= FLAG_BYPASS_SURFACE_CACHE
;
999 MOZ_ASSERT(!(aFlags
& FLAG_FORCE_PRESERVEASPECTRATIO_NONE
) ||
1000 aSVGContext
.GetViewportSize(),
1001 "Viewport size is required when using "
1002 "FLAG_FORCE_PRESERVEASPECTRATIO_NONE");
1004 uint32_t whichFrame
= mHaveAnimations
? aWhichFrame
: FRAME_FIRST
;
1006 float animTime
= (whichFrame
== FRAME_FIRST
)
1008 : mSVGDocumentWrapper
->GetCurrentTimeAsFloat();
1010 SVGImageContext newSVGContext
= aSVGContext
;
1011 bool contextPaint
= MaybeRestrictSVGContext(newSVGContext
, aFlags
);
1013 SVGDrawingParameters
params(aContext
, aSize
, aSize
, aRegion
, aSamplingFilter
,
1014 newSVGContext
, animTime
, aFlags
, aOpacity
);
1016 // If we have an prerasterized version of this image that matches the
1017 // drawing parameters, use that.
1018 RefPtr
<SourceSurface
> sourceSurface
;
1019 std::tie(sourceSurface
, params
.size
) =
1020 LookupCachedSurface(aSize
, params
.svgContext
, aFlags
);
1021 if (sourceSurface
) {
1022 RefPtr
<gfxDrawable
> drawable
=
1023 new gfxSurfaceDrawable(sourceSurface
, params
.size
);
1024 Show(drawable
, params
);
1025 return ImgDrawResult::SUCCESS
;
1028 // else, we need to paint the image:
1030 if (mSVGDocumentWrapper
->IsDrawing()) {
1031 NS_WARNING("Refusing to make re-entrant call to VectorImage::Draw");
1032 return ImgDrawResult::TEMPORARY_ERROR
;
1035 AutoRestoreSVGState
autoRestore(params
, mSVGDocumentWrapper
, contextPaint
);
1037 bool didCache
; // Was the surface put into the cache?
1038 RefPtr
<gfxDrawable
> svgDrawable
= CreateSVGDrawable(params
);
1039 sourceSurface
= CreateSurface(params
, svgDrawable
, didCache
);
1040 if (!sourceSurface
) {
1041 MOZ_ASSERT(!didCache
);
1042 Show(svgDrawable
, params
);
1043 return ImgDrawResult::SUCCESS
;
1046 RefPtr
<gfxDrawable
> drawable
=
1047 new gfxSurfaceDrawable(sourceSurface
, params
.size
);
1048 Show(drawable
, params
);
1049 SendFrameComplete(didCache
, params
.flags
);
1050 return ImgDrawResult::SUCCESS
;
1053 already_AddRefed
<gfxDrawable
> VectorImage::CreateSVGDrawable(
1054 const SVGDrawingParameters
& aParams
) {
1055 RefPtr
<gfxDrawingCallback
> cb
= new SVGDrawingCallback(
1056 mSVGDocumentWrapper
, aParams
.viewportSize
, aParams
.size
, aParams
.flags
);
1058 RefPtr
<gfxDrawable
> svgDrawable
= new gfxCallbackDrawable(cb
, aParams
.size
);
1059 return svgDrawable
.forget();
1062 std::tuple
<RefPtr
<SourceSurface
>, IntSize
> VectorImage::LookupCachedSurface(
1063 const IntSize
& aSize
, const SVGImageContext
& aSVGContext
, uint32_t aFlags
) {
1064 // We can't use cached surfaces if we:
1065 // - Explicitly disallow it via FLAG_BYPASS_SURFACE_CACHE
1066 // - Want a blob recording which aren't supported by the cache.
1067 // - Have animations which aren't supported by the cache.
1068 if (aFlags
& (FLAG_BYPASS_SURFACE_CACHE
| FLAG_RECORD_BLOB
) ||
1070 return std::make_tuple(RefPtr
<SourceSurface
>(), aSize
);
1073 LookupResult
result(MatchType::NOT_FOUND
);
1074 SurfaceKey surfaceKey
= VectorSurfaceKey(aSize
, aSVGContext
);
1075 if ((aFlags
& FLAG_SYNC_DECODE
) || !(aFlags
& FLAG_HIGH_QUALITY_SCALING
)) {
1076 result
= SurfaceCache::Lookup(ImageKey(this), surfaceKey
,
1077 /* aMarkUsed = */ true);
1079 result
= SurfaceCache::LookupBestMatch(ImageKey(this), surfaceKey
,
1080 /* aMarkUsed = */ true);
1083 IntSize rasterSize
=
1084 result
.SuggestedSize().IsEmpty() ? aSize
: result
.SuggestedSize();
1085 MOZ_ASSERT(result
.Type() != MatchType::SUBSTITUTE_BECAUSE_PENDING
);
1086 if (!result
|| result
.Type() == MatchType::SUBSTITUTE_BECAUSE_NOT_FOUND
) {
1087 // No matching surface, or the OS freed the volatile buffer.
1088 return std::make_tuple(RefPtr
<SourceSurface
>(), rasterSize
);
1091 RefPtr
<SourceSurface
> sourceSurface
= result
.Surface()->GetSourceSurface();
1092 if (!sourceSurface
) {
1093 // Something went wrong. (Probably a GPU driver crash or device reset.)
1094 // Attempt to recover.
1095 RecoverFromLossOfSurfaces();
1096 return std::make_tuple(RefPtr
<SourceSurface
>(), rasterSize
);
1099 return std::make_tuple(std::move(sourceSurface
), rasterSize
);
1102 already_AddRefed
<SourceSurface
> VectorImage::CreateSurface(
1103 const SVGDrawingParameters
& aParams
, gfxDrawable
* aSVGDrawable
,
1105 MOZ_ASSERT(mSVGDocumentWrapper
->IsDrawing());
1106 MOZ_ASSERT(!(aParams
.flags
& FLAG_RECORD_BLOB
));
1108 mSVGDocumentWrapper
->UpdateViewportBounds(aParams
.viewportSize
);
1109 mSVGDocumentWrapper
->FlushImageTransformInvalidation();
1111 // Determine whether or not we should put the surface to be created into
1112 // the cache. If we fail, we need to reset this to false to let the caller
1113 // know nothing was put in the cache.
1114 aWillCache
= !(aParams
.flags
& FLAG_BYPASS_SURFACE_CACHE
) &&
1115 // Refuse to cache animated images:
1116 // XXX(seth): We may remove this restriction in bug 922893.
1118 // The image is too big to fit in the cache:
1119 SurfaceCache::CanHold(aParams
.size
);
1121 // If we weren't given a context, then we know we just want the rasterized
1122 // surface. We will create the frame below but only insert it into the cache
1123 // if we actually need to.
1124 if (!aWillCache
&& aParams
.context
) {
1128 // We're about to rerasterize, which may mean that some of the previous
1129 // surfaces we've rasterized aren't useful anymore. We can allow them to
1130 // expire from the cache by unlocking them here, and then sending out an
1131 // invalidation. If this image is locked, any surfaces that are still useful
1132 // will become locked again when Draw touches them, and the remainder will
1133 // eventually expire.
1135 SurfaceCache::UnlockEntries(ImageKey(this));
1138 // If there is no context, the default backend is fine.
1139 BackendType backend
=
1140 aParams
.context
? aParams
.context
->GetDrawTarget()->GetBackendType()
1141 : gfxPlatform::GetPlatform()->GetDefaultContentBackend();
1143 if (backend
== BackendType::DIRECT2D1_1
) {
1144 // We don't want to draw arbitrary content with D2D anymore
1145 // because it doesn't support PushLayerWithBlend so switch to skia
1146 backend
= BackendType::SKIA
;
1149 // Try to create an imgFrame, initializing the surface it contains by drawing
1150 // our gfxDrawable into it. (We use FILTER_NEAREST since we never scale here.)
1151 auto frame
= MakeNotNull
<RefPtr
<imgFrame
>>();
1152 nsresult rv
= frame
->InitWithDrawable(
1153 aSVGDrawable
, aParams
.size
, SurfaceFormat::OS_RGBA
, SamplingFilter::POINT
,
1154 aParams
.flags
, backend
);
1156 // If we couldn't create the frame, it was probably because it would end
1157 // up way too big. Generally it also wouldn't fit in the cache, but the prefs
1158 // could be set such that the cache isn't the limiting factor.
1159 if (NS_FAILED(rv
)) {
1164 // Take a strong reference to the frame's surface and make sure it hasn't
1165 // already been purged by the operating system.
1166 RefPtr
<SourceSurface
> surface
= frame
->GetSourceSurface();
1172 // We created the frame, but only because we had no context to draw to
1173 // directly. All the caller wants is the surface in this case.
1175 return surface
.forget();
1178 // Attempt to cache the frame.
1179 SurfaceKey surfaceKey
= VectorSurfaceKey(aParams
.size
, aParams
.svgContext
);
1180 NotNull
<RefPtr
<ISurfaceProvider
>> provider
=
1181 MakeNotNull
<SimpleSurfaceProvider
*>(ImageKey(this), surfaceKey
, frame
);
1183 if (SurfaceCache::Insert(provider
) == InsertOutcome::SUCCESS
) {
1184 if (aParams
.size
!= aParams
.drawSize
) {
1185 // We created a new surface that wasn't the size we requested, which means
1186 // we entered factor-of-2 mode. We should purge any surfaces we no longer
1187 // need rather than waiting for the cache to expire them.
1188 SurfaceCache::PruneImage(ImageKey(this));
1194 return surface
.forget();
1197 void VectorImage::SendFrameComplete(bool aDidCache
, uint32_t aFlags
) {
1198 // If the cache was not updated, we have nothing to do.
1203 // Send out an invalidation so that surfaces that are still in use get
1204 // re-locked. See the discussion of the UnlockSurfaces call above.
1205 if (!(aFlags
& FLAG_ASYNC_NOTIFY
)) {
1206 mProgressTracker
->SyncNotifyProgress(FLAG_FRAME_COMPLETE
,
1207 GetMaxSizedIntRect());
1209 NotNull
<RefPtr
<VectorImage
>> image
= WrapNotNull(this);
1210 NS_DispatchToMainThread(CreateRenderBlockingRunnable(NS_NewRunnableFunction(
1211 "ProgressTracker::SyncNotifyProgress", [=]() -> void {
1212 RefPtr
<ProgressTracker
> tracker
= image
->GetProgressTracker();
1214 tracker
->SyncNotifyProgress(FLAG_FRAME_COMPLETE
,
1215 GetMaxSizedIntRect());
1221 void VectorImage::Show(gfxDrawable
* aDrawable
,
1222 const SVGDrawingParameters
& aParams
) {
1223 // The surface size may differ from the size at which we wish to draw. As
1224 // such, we may need to adjust the context/region to take this into account.
1225 gfxContextMatrixAutoSaveRestore
saveMatrix(aParams
.context
);
1226 ImageRegion
region(aParams
.region
);
1227 if (aParams
.drawSize
!= aParams
.size
) {
1228 gfx::MatrixScales
scale(
1229 double(aParams
.drawSize
.width
) / aParams
.size
.width
,
1230 double(aParams
.drawSize
.height
) / aParams
.size
.height
);
1231 aParams
.context
->Multiply(gfx::Matrix::Scaling(scale
));
1232 region
.Scale(1.0 / scale
.xScale
, 1.0 / scale
.yScale
);
1235 MOZ_ASSERT(aDrawable
, "Should have a gfxDrawable by now");
1236 gfxUtils::DrawPixelSnapped(aParams
.context
, aDrawable
,
1237 SizeDouble(aParams
.size
), region
,
1238 SurfaceFormat::OS_RGBA
, aParams
.samplingFilter
,
1239 aParams
.flags
, aParams
.opacity
, false);
1241 AutoProfilerImagePaintMarker
PROFILER_RAII(this);
1243 NotifyDrawingObservers();
1246 MOZ_ASSERT(mRenderingObserver
, "Should have a rendering observer by now");
1247 mRenderingObserver
->ResumeHonoringInvalidations();
1250 void VectorImage::RecoverFromLossOfSurfaces() {
1251 NS_WARNING("An imgFrame became invalid. Attempting to recover...");
1253 // Discard all existing frames, since they're probably all now invalid.
1254 SurfaceCache::RemoveImage(ImageKey(this));
1258 VectorImage::StartDecoding(uint32_t aFlags
, uint32_t aWhichFrame
) {
1259 // Nothing to do for SVG images
1263 bool VectorImage::StartDecodingWithResult(uint32_t aFlags
,
1264 uint32_t aWhichFrame
) {
1265 // SVG images are ready to draw when they are loaded
1266 return mIsFullyLoaded
;
1269 bool VectorImage::HasDecodedPixels() {
1270 MOZ_ASSERT_UNREACHABLE("calling VectorImage::HasDecodedPixels");
1271 return mIsFullyLoaded
;
1274 imgIContainer::DecodeResult
VectorImage::RequestDecodeWithResult(
1275 uint32_t aFlags
, uint32_t aWhichFrame
) {
1276 // SVG images are ready to draw when they are loaded and don't have an error.
1279 return imgIContainer::DECODE_REQUEST_FAILED
;
1282 if (!mIsFullyLoaded
) {
1283 return imgIContainer::DECODE_REQUESTED
;
1286 return imgIContainer::DECODE_SURFACE_AVAILABLE
;
1290 VectorImage::RequestDecodeForSize(const nsIntSize
& aSize
, uint32_t aFlags
,
1291 uint32_t aWhichFrame
) {
1292 // Nothing to do for SVG images, though in theory we could rasterize to the
1293 // provided size ahead of time if we supported off-main-thread SVG
1298 //******************************************************************************
1301 VectorImage::LockImage() {
1302 MOZ_ASSERT(NS_IsMainThread());
1305 return NS_ERROR_FAILURE
;
1310 if (mLockCount
== 1) {
1311 // Lock this image's surfaces in the SurfaceCache.
1312 SurfaceCache::LockImage(ImageKey(this));
1318 //******************************************************************************
1321 VectorImage::UnlockImage() {
1322 MOZ_ASSERT(NS_IsMainThread());
1325 return NS_ERROR_FAILURE
;
1328 if (mLockCount
== 0) {
1329 MOZ_ASSERT_UNREACHABLE("Calling UnlockImage with a zero lock count");
1330 return NS_ERROR_ABORT
;
1335 if (mLockCount
== 0) {
1336 // Unlock this image's surfaces in the SurfaceCache.
1337 SurfaceCache::UnlockImage(ImageKey(this));
1343 //******************************************************************************
1346 VectorImage::RequestDiscard() {
1347 MOZ_ASSERT(NS_IsMainThread());
1349 if (mDiscardable
&& mLockCount
== 0) {
1350 SurfaceCache::RemoveImage(ImageKey(this));
1351 mProgressTracker
->OnDiscard();
1357 void VectorImage::OnSurfaceDiscarded(const SurfaceKey
& aSurfaceKey
) {
1358 MOZ_ASSERT(mProgressTracker
);
1360 NS_DispatchToMainThread(NewRunnableMethod("ProgressTracker::OnDiscard",
1362 &ProgressTracker::OnDiscard
));
1365 //******************************************************************************
1367 VectorImage::ResetAnimation() {
1369 return NS_ERROR_FAILURE
;
1372 if (!mIsFullyLoaded
|| !mHaveAnimations
) {
1373 return NS_OK
; // There are no animations to be reset.
1376 mSVGDocumentWrapper
->ResetAnimation();
1381 NS_IMETHODIMP_(float)
1382 VectorImage::GetFrameIndex(uint32_t aWhichFrame
) {
1383 MOZ_ASSERT(aWhichFrame
<= FRAME_MAX_VALUE
, "Invalid argument");
1384 return aWhichFrame
== FRAME_FIRST
1386 : mSVGDocumentWrapper
->GetCurrentTimeAsFloat();
1389 //------------------------------------------------------------------------------
1390 // nsIRequestObserver methods
1392 //******************************************************************************
1394 VectorImage::OnStartRequest(nsIRequest
* aRequest
) {
1395 MOZ_ASSERT(!mSVGDocumentWrapper
,
1396 "Repeated call to OnStartRequest -- can this happen?");
1398 mSVGDocumentWrapper
= new SVGDocumentWrapper();
1399 nsresult rv
= mSVGDocumentWrapper
->OnStartRequest(aRequest
);
1400 if (NS_FAILED(rv
)) {
1401 mSVGDocumentWrapper
= nullptr;
1406 // Create a listener to wait until the SVG document is fully loaded, which
1407 // will signal that this image is ready to render. Certain error conditions
1408 // will prevent us from ever getting this notification, so we also create a
1409 // listener that waits for parsing to complete and cancels the
1410 // SVGLoadEventListener if needed. The listeners are automatically attached
1411 // to the document by their constructors.
1412 SVGDocument
* document
= mSVGDocumentWrapper
->GetDocument();
1413 mLoadEventListener
= new SVGLoadEventListener(document
, this);
1414 mParseCompleteListener
= new SVGParseCompleteListener(document
, this);
1416 // Displayed documents will call InitUseCounters under SetScriptGlobalObject,
1417 // but SVG image documents never get a script global object, so we initialize
1418 // use counters here, right after the document has been created.
1419 document
->InitUseCounters();
1424 //******************************************************************************
1426 VectorImage::OnStopRequest(nsIRequest
* aRequest
, nsresult aStatus
) {
1428 return NS_ERROR_FAILURE
;
1431 return mSVGDocumentWrapper
->OnStopRequest(aRequest
, aStatus
);
1434 void VectorImage::OnSVGDocumentParsed() {
1435 MOZ_ASSERT(mParseCompleteListener
, "Should have the parse complete listener");
1436 MOZ_ASSERT(mLoadEventListener
, "Should have the load event listener");
1438 if (!mSVGDocumentWrapper
->GetRootSVGElem()) {
1439 // This is an invalid SVG document. It may have failed to parse, or it may
1440 // be missing the <svg> root element, or the <svg> root element may not
1441 // declare the correct namespace. In any of these cases, we'll never be
1442 // notified that the SVG finished loading, so we need to treat this as an
1444 OnSVGDocumentError();
1448 void VectorImage::CancelAllListeners() {
1449 if (mParseCompleteListener
) {
1450 mParseCompleteListener
->Cancel();
1451 mParseCompleteListener
= nullptr;
1453 if (mLoadEventListener
) {
1454 mLoadEventListener
->Cancel();
1455 mLoadEventListener
= nullptr;
1459 void VectorImage::OnSVGDocumentLoaded() {
1460 MOZ_ASSERT(mSVGDocumentWrapper
->GetRootSVGElem(),
1461 "Should have parsed successfully");
1462 MOZ_ASSERT(!mIsFullyLoaded
&& !mHaveAnimations
,
1463 "These flags shouldn't get set until OnSVGDocumentLoaded. "
1464 "Duplicate calls to OnSVGDocumentLoaded?");
1466 CancelAllListeners();
1468 // XXX Flushing is wasteful if embedding frame hasn't had initial reflow.
1469 mSVGDocumentWrapper
->FlushLayout();
1471 // This is the earliest point that we can get accurate use counter data
1472 // for a valid SVG document. Without the FlushLayout call, we would miss
1473 // any CSS property usage that comes from SVG presentation attributes.
1474 mSVGDocumentWrapper
->GetDocument()->ReportDocumentUseCounters();
1476 mIsFullyLoaded
= true;
1477 mHaveAnimations
= mSVGDocumentWrapper
->IsAnimated();
1479 // Start listening to our image for rendering updates.
1480 mRenderingObserver
= new SVGRootRenderingObserver(mSVGDocumentWrapper
, this);
1482 // ProgressTracker::SyncNotifyProgress may release us, so ensure we
1483 // stick around long enough to complete our work.
1484 RefPtr
<VectorImage
> kungFuDeathGrip(this);
1486 // Tell *our* observers that we're done loading.
1487 if (mProgressTracker
) {
1488 Progress progress
= FLAG_SIZE_AVAILABLE
| FLAG_HAS_TRANSPARENCY
|
1489 FLAG_FRAME_COMPLETE
| FLAG_DECODE_COMPLETE
;
1491 if (mHaveAnimations
) {
1492 progress
|= FLAG_IS_ANIMATED
;
1495 // Merge in any saved progress from OnImageDataComplete.
1496 if (mLoadProgress
) {
1497 progress
|= *mLoadProgress
;
1498 mLoadProgress
= Nothing();
1501 mProgressTracker
->SyncNotifyProgress(progress
, GetMaxSizedIntRect());
1504 EvaluateAnimation();
1507 void VectorImage::OnSVGDocumentError() {
1508 CancelAllListeners();
1512 // We won't enter OnSVGDocumentLoaded, so report use counters now for this
1513 // invalid document.
1514 ReportDocumentUseCounters();
1516 if (mProgressTracker
) {
1517 // Notify observers about the error and unblock page load.
1518 Progress progress
= FLAG_HAS_ERROR
;
1520 // Merge in any saved progress from OnImageDataComplete.
1521 if (mLoadProgress
) {
1522 progress
|= *mLoadProgress
;
1523 mLoadProgress
= Nothing();
1526 mProgressTracker
->SyncNotifyProgress(progress
);
1530 //------------------------------------------------------------------------------
1531 // nsIStreamListener method
1533 //******************************************************************************
1535 VectorImage::OnDataAvailable(nsIRequest
* aRequest
, nsIInputStream
* aInStr
,
1536 uint64_t aSourceOffset
, uint32_t aCount
) {
1538 return NS_ERROR_FAILURE
;
1541 return mSVGDocumentWrapper
->OnDataAvailable(aRequest
, aInStr
, aSourceOffset
,
1545 // --------------------------
1546 // Invalidation helper method
1548 void VectorImage::InvalidateObserversOnNextRefreshDriverTick() {
1549 if (mHasPendingInvalidation
) {
1553 mHasPendingInvalidation
= true;
1555 // Animated images can wait for the refresh tick.
1556 if (mHaveAnimations
) {
1560 // Non-animated images won't get the refresh tick, so we should just send an
1561 // invalidation outside the current execution context. We need to defer
1562 // because the layout tree is in the middle of invalidation, and the tree
1563 // state needs to be consistent. Specifically only some of the frames have
1564 // had the NS_FRAME_DESCENDANT_NEEDS_PAINT and/or NS_FRAME_NEEDS_PAINT bits
1565 // set by InvalidateFrameInternal in layout/generic/nsFrame.cpp. These bits
1566 // get cleared when we repaint the SVG into a surface by
1567 // nsIFrame::ClearInvalidationStateBits in nsDisplayList::PaintRoot.
1568 nsCOMPtr
<nsIEventTarget
> eventTarget
= do_GetMainThread();
1570 RefPtr
<VectorImage
> self(this);
1571 nsCOMPtr
<nsIRunnable
> ev(NS_NewRunnableFunction(
1572 "VectorImage::SendInvalidationNotifications",
1573 [=]() -> void { self
->SendInvalidationNotifications(); }));
1574 eventTarget
->Dispatch(CreateRenderBlockingRunnable(ev
.forget()),
1575 NS_DISPATCH_NORMAL
);
1578 void VectorImage::PropagateUseCounters(Document
* aReferencingDocument
) {
1579 if (Document
* doc
= mSVGDocumentWrapper
->GetDocument()) {
1580 doc
->PropagateImageUseCounters(aReferencingDocument
);
1584 nsIntSize
VectorImage::OptimalImageSizeForDest(const gfxSize
& aDest
,
1585 uint32_t aWhichFrame
,
1586 SamplingFilter aSamplingFilter
,
1588 MOZ_ASSERT(aDest
.width
>= 0 || ceil(aDest
.width
) <= INT32_MAX
||
1589 aDest
.height
>= 0 || ceil(aDest
.height
) <= INT32_MAX
,
1590 "Unexpected destination size");
1592 // We can rescale SVGs freely, so just return the provided destination size.
1593 return nsIntSize::Ceil(aDest
.width
, aDest
.height
);
1596 already_AddRefed
<imgIContainer
> VectorImage::Unwrap() {
1597 nsCOMPtr
<imgIContainer
> self(this);
1598 return self
.forget();
1601 void VectorImage::MediaFeatureValuesChangedAllDocuments(
1602 const MediaFeatureChange
& aChange
) {
1603 if (!mSVGDocumentWrapper
) {
1607 // Don't bother if the document hasn't loaded yet.
1608 if (!mIsFullyLoaded
) {
1612 if (Document
* doc
= mSVGDocumentWrapper
->GetDocument()) {
1613 if (RefPtr
<nsPresContext
> presContext
= doc
->GetPresContext()) {
1614 presContext
->MediaFeatureValuesChanged(
1615 aChange
, MediaFeatureChangePropagation::All
);
1616 // Media feature value changes don't happen in the middle of layout,
1617 // so we don't need to call InvalidateObserversOnNextRefreshDriverTick
1618 // to invalidate asynchronously.
1619 if (presContext
->FlushPendingMediaFeatureValuesChanged()) {
1620 // NOTE(emilio): SendInvalidationNotifications flushes layout via
1621 // VectorImage::CreateSurface -> FlushImageTransformInvalidation.
1622 SendInvalidationNotifications();
1628 nsresult
VectorImage::GetHotspotX(int32_t* aX
) {
1629 return Image::GetHotspotX(aX
);
1632 nsresult
VectorImage::GetHotspotY(int32_t* aY
) {
1633 return Image::GetHotspotY(aY
);
1636 void VectorImage::ReportDocumentUseCounters() {
1637 if (!mSVGDocumentWrapper
) {
1641 if (Document
* doc
= mSVGDocumentWrapper
->GetDocument()) {
1642 doc
->ReportDocumentUseCounters();
1646 } // namespace image
1647 } // namespace mozilla