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"
9 #include "gfxContext.h"
10 #include "gfxDrawable.h"
11 #include "gfxPlatform.h"
14 #include "mozilla/AutoRestore.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/gfx/gfxVars.h"
22 #include "mozilla/layers/LayerManager.h"
23 #include "mozilla/PendingAnimationTracker.h"
24 #include "mozilla/PresShell.h"
25 #include "mozilla/ProfilerLabels.h"
26 #include "mozilla/RefPtr.h"
27 #include "mozilla/SVGObserverUtils.h" // for SVGRenderingObserver
28 #include "mozilla/Tuple.h"
29 #include "nsIStreamListener.h"
30 #include "nsMimeTypes.h"
31 #include "nsPresContext.h"
34 #include "nsStubDocumentObserver.h"
35 #include "nsWindowSizes.h"
36 #include "ImageRegion.h"
37 #include "ISurfaceProvider.h"
38 #include "LookupResult.h"
39 #include "Orientation.h"
40 #include "SVGDocumentWrapper.h"
41 #include "SVGDrawingParameters.h"
42 #include "nsIDOMEventListener.h"
43 #include "SurfaceCache.h"
44 #include "mozilla/dom/Document.h"
45 #include "mozilla/dom/DocumentInlines.h"
50 using namespace dom::SVGPreserveAspectRatio_Binding
;
52 using namespace layers
;
56 // Helper-class: SVGRootRenderingObserver
57 class SVGRootRenderingObserver final
: public SVGRenderingObserver
{
61 SVGRootRenderingObserver(SVGDocumentWrapper
* aDocWrapper
,
62 VectorImage
* aVectorImage
)
63 : SVGRenderingObserver(),
64 mDocWrapper(aDocWrapper
),
65 mVectorImage(aVectorImage
),
66 mHonoringInvalidations(true) {
67 MOZ_ASSERT(mDocWrapper
, "Need a non-null SVG document wrapper");
68 MOZ_ASSERT(mVectorImage
, "Need a non-null VectorImage");
71 Element
* elem
= GetReferencedElementWithoutObserving();
72 MOZ_ASSERT(elem
, "no root SVG node for us to observe");
74 SVGObserverUtils::AddRenderingObserver(elem
, this);
75 mInObserverSet
= true;
78 void ResumeHonoringInvalidations() { mHonoringInvalidations
= true; }
81 virtual ~SVGRootRenderingObserver() {
82 // This needs to call our GetReferencedElementWithoutObserving override,
83 // so must be called here rather than in our base class's dtor.
87 Element
* GetReferencedElementWithoutObserving() final
{
88 return mDocWrapper
->GetRootSVGElem();
91 virtual void OnRenderingChange() override
{
92 Element
* elem
= GetReferencedElementWithoutObserving();
93 MOZ_ASSERT(elem
, "missing root SVG node");
95 if (mHonoringInvalidations
&& !mDocWrapper
->ShouldIgnoreInvalidation()) {
96 nsIFrame
* frame
= elem
->GetPrimaryFrame();
97 if (!frame
|| frame
->PresShell()->IsDestroying()) {
98 // We're being destroyed. Bail out.
102 // Ignore further invalidations until we draw.
103 mHonoringInvalidations
= false;
105 mVectorImage
->InvalidateObserversOnNextRefreshDriverTick();
108 // Our caller might've removed us from rendering-observer list.
109 // Add ourselves back!
110 if (!mInObserverSet
) {
111 SVGObserverUtils::AddRenderingObserver(elem
, this);
112 mInObserverSet
= true;
117 const RefPtr
<SVGDocumentWrapper
> mDocWrapper
;
118 VectorImage
* const mVectorImage
; // Raw pointer because it owns me.
119 bool mHonoringInvalidations
;
122 NS_IMPL_ISUPPORTS(SVGRootRenderingObserver
, nsIMutationObserver
)
124 class SVGParseCompleteListener final
: public nsStubDocumentObserver
{
128 SVGParseCompleteListener(SVGDocument
* aDocument
, VectorImage
* aImage
)
129 : mDocument(aDocument
), mImage(aImage
) {
130 MOZ_ASSERT(mDocument
, "Need an SVG document");
131 MOZ_ASSERT(mImage
, "Need an image");
133 mDocument
->AddObserver(this);
137 ~SVGParseCompleteListener() {
139 // The document must have been destroyed before we got our event.
140 // Otherwise this can't happen, since documents hold strong references to
147 void EndLoad(Document
* aDocument
) override
{
148 MOZ_ASSERT(aDocument
== mDocument
, "Got EndLoad for wrong document?");
150 // OnSVGDocumentParsed will release our owner's reference to us, so ensure
151 // we stick around long enough to complete our work.
152 RefPtr
<SVGParseCompleteListener
> kungFuDeathGrip(this);
154 mImage
->OnSVGDocumentParsed();
158 MOZ_ASSERT(mDocument
, "Duplicate call to Cancel");
160 mDocument
->RemoveObserver(this);
166 RefPtr
<SVGDocument
> mDocument
;
167 VectorImage
* const mImage
; // Raw pointer to owner.
170 NS_IMPL_ISUPPORTS(SVGParseCompleteListener
, nsIDocumentObserver
)
172 class SVGLoadEventListener final
: public nsIDOMEventListener
{
176 SVGLoadEventListener(Document
* aDocument
, VectorImage
* aImage
)
177 : mDocument(aDocument
), mImage(aImage
) {
178 MOZ_ASSERT(mDocument
, "Need an SVG document");
179 MOZ_ASSERT(mImage
, "Need an image");
181 mDocument
->AddEventListener(u
"MozSVGAsImageDocumentLoad"_ns
, this, true,
186 ~SVGLoadEventListener() {
188 // The document must have been destroyed before we got our event.
189 // Otherwise this can't happen, since documents hold strong references to
196 NS_IMETHOD
HandleEvent(Event
* aEvent
) override
{
197 MOZ_ASSERT(mDocument
, "Need an SVG document. Received multiple events?");
199 // OnSVGDocumentLoaded will release our owner's reference
200 // to us, so ensure we stick around long enough to complete our work.
201 RefPtr
<SVGLoadEventListener
> kungFuDeathGrip(this);
204 nsAutoString eventType
;
205 aEvent
->GetType(eventType
);
206 MOZ_ASSERT(eventType
.EqualsLiteral("MozSVGAsImageDocumentLoad"),
207 "Received unexpected event");
210 mImage
->OnSVGDocumentLoaded();
216 MOZ_ASSERT(mDocument
, "Duplicate call to Cancel");
218 mDocument
->RemoveEventListener(u
"MozSVGAsImageDocumentLoad"_ns
, this,
225 nsCOMPtr
<Document
> mDocument
;
226 VectorImage
* const mImage
; // Raw pointer to owner.
229 NS_IMPL_ISUPPORTS(SVGLoadEventListener
, nsIDOMEventListener
)
231 // Helper-class: SVGDrawingCallback
232 class SVGDrawingCallback
: public gfxDrawingCallback
{
234 SVGDrawingCallback(SVGDocumentWrapper
* aSVGDocumentWrapper
,
235 const IntSize
& aViewportSize
, const IntSize
& aSize
,
236 uint32_t aImageFlags
)
237 : mSVGDocumentWrapper(aSVGDocumentWrapper
),
238 mViewportSize(aViewportSize
),
240 mImageFlags(aImageFlags
) {}
241 virtual bool operator()(gfxContext
* aContext
, const gfxRect
& aFillRect
,
242 const SamplingFilter aSamplingFilter
,
243 const gfxMatrix
& aTransform
) override
;
246 RefPtr
<SVGDocumentWrapper
> mSVGDocumentWrapper
;
247 const IntSize mViewportSize
;
249 uint32_t mImageFlags
;
252 // Based loosely on SVGIntegrationUtils' PaintFrameCallback::operator()
253 bool SVGDrawingCallback::operator()(gfxContext
* aContext
,
254 const gfxRect
& aFillRect
,
255 const SamplingFilter aSamplingFilter
,
256 const gfxMatrix
& aTransform
) {
257 MOZ_ASSERT(mSVGDocumentWrapper
, "need an SVGDocumentWrapper");
259 // Get (& sanity-check) the helper-doc's presShell
260 RefPtr
<PresShell
> presShell
= mSVGDocumentWrapper
->GetPresShell();
261 MOZ_ASSERT(presShell
, "GetPresShell returned null for an SVG image?");
263 #ifdef MOZ_GECKO_PROFILER
264 Document
* doc
= presShell
->GetDocument();
265 nsIURI
* uri
= doc
? doc
->GetDocumentURI() : nullptr;
266 AUTO_PROFILER_LABEL_DYNAMIC_NSCSTRING(
267 "SVG Image drawing", GRAPHICS
,
268 nsPrintfCString("%dx%d %s", mSize
.width
, mSize
.height
,
269 uri
? uri
->GetSpecOrDefault().get() : "N/A"));
272 gfxContextAutoSaveRestore
contextRestorer(aContext
);
274 // Clip to aFillRect so that we don't paint outside.
276 aContext
->Rectangle(aFillRect
);
279 gfxMatrix matrix
= aTransform
;
280 if (!matrix
.Invert()) {
283 aContext
->SetMatrixDouble(
284 aContext
->CurrentMatrixDouble().PreMultiply(matrix
).PreScale(
285 double(mSize
.width
) / mViewportSize
.width
,
286 double(mSize
.height
) / mViewportSize
.height
));
288 nsPresContext
* presContext
= presShell
->GetPresContext();
289 MOZ_ASSERT(presContext
, "pres shell w/out pres context");
291 nsRect
svgRect(0, 0, presContext
->DevPixelsToAppUnits(mViewportSize
.width
),
292 presContext
->DevPixelsToAppUnits(mViewportSize
.height
));
294 RenderDocumentFlags renderDocFlags
=
295 RenderDocumentFlags::IgnoreViewportScrolling
;
296 if (!(mImageFlags
& imgIContainer::FLAG_SYNC_DECODE
)) {
297 renderDocFlags
|= RenderDocumentFlags::AsyncDecodeImages
;
299 if (mImageFlags
& imgIContainer::FLAG_HIGH_QUALITY_SCALING
) {
300 renderDocFlags
|= RenderDocumentFlags::UseHighQualityScaling
;
303 presShell
->RenderDocument(svgRect
, renderDocFlags
,
304 NS_RGBA(0, 0, 0, 0), // transparent
310 class MOZ_STACK_CLASS AutoRestoreSVGState final
{
312 AutoRestoreSVGState(const SVGDrawingParameters
& aParams
,
313 SVGDocumentWrapper
* aSVGDocumentWrapper
, bool& aIsDrawing
,
315 : mIsDrawing(aIsDrawing
)
316 // Apply any 'preserveAspectRatio' override (if specified) to the root
319 mPAR(aParams
.svgContext
, aSVGDocumentWrapper
->GetRootSVGElem())
320 // Set the animation time:
322 mTime(aSVGDocumentWrapper
->GetRootSVGElem(), aParams
.animationTime
) {
323 MOZ_ASSERT(!aIsDrawing
);
324 MOZ_ASSERT(aSVGDocumentWrapper
->GetDocument());
328 // Set context paint (if specified) on the document:
330 MOZ_ASSERT(aParams
.svgContext
->GetContextPaint());
331 mContextPaint
.emplace(*aParams
.svgContext
->GetContextPaint(),
332 *aSVGDocumentWrapper
->GetDocument());
337 AutoRestore
<bool> mIsDrawing
;
338 AutoPreserveAspectRatioOverride mPAR
;
339 AutoSVGTimeSetRestore mTime
;
340 Maybe
<AutoSetRestoreSVGContextPaint
> mContextPaint
;
343 // Implement VectorImage's nsISupports-inherited methods
344 NS_IMPL_ISUPPORTS(VectorImage
, imgIContainer
, nsIStreamListener
,
347 //------------------------------------------------------------------------------
348 // Constructor / Destructor
350 VectorImage::VectorImage(nsIURI
* aURI
/* = nullptr */)
351 : ImageResource(aURI
), // invoke superclass's constructor
353 mIsInitialized(false),
355 mIsFullyLoaded(false),
357 mHaveAnimations(false),
358 mHasPendingInvalidation(false) {}
360 VectorImage::~VectorImage() {
361 ReportDocumentUseCounters();
362 CancelAllListeners();
363 SurfaceCache::RemoveImage(ImageKey(this));
366 //------------------------------------------------------------------------------
367 // Methods inherited from Image.h
369 nsresult
VectorImage::Init(const char* aMimeType
, uint32_t aFlags
) {
370 // We don't support re-initialization
371 if (mIsInitialized
) {
372 return NS_ERROR_ILLEGAL_VALUE
;
375 MOZ_ASSERT(!mIsFullyLoaded
&& !mHaveAnimations
&& !mError
,
376 "Flags unexpectedly set before initialization");
377 MOZ_ASSERT(!strcmp(aMimeType
, IMAGE_SVG_XML
), "Unexpected mimetype");
379 mDiscardable
= !!(aFlags
& INIT_FLAG_DISCARDABLE
);
381 // Lock this image's surfaces in the SurfaceCache if we're not discardable.
384 SurfaceCache::LockImage(ImageKey(this));
387 mIsInitialized
= true;
391 size_t VectorImage::SizeOfSourceWithComputedFallback(
392 SizeOfState
& aState
) const {
393 if (!mSVGDocumentWrapper
) {
394 return 0; // No document, so no memory used for the document.
397 SVGDocument
* doc
= mSVGDocumentWrapper
->GetDocument();
399 return 0; // No document, so no memory used for the document.
402 nsWindowSizes
windowSizes(aState
);
403 doc
->DocAddSizeOfIncludingThis(windowSizes
);
405 if (windowSizes
.getTotalSize() == 0) {
406 // MallocSizeOf fails on this platform. Because we also use this method for
407 // determining the size of cache entries, we need to return something
408 // reasonable here. Unfortunately, there's no way to estimate the document's
409 // size accurately, so we just use a constant value of 100KB, which will
410 // generally underestimate the true size.
414 return windowSizes
.getTotalSize();
417 void VectorImage::CollectSizeOfSurfaces(
418 nsTArray
<SurfaceMemoryCounter
>& aCounters
,
419 MallocSizeOf aMallocSizeOf
) const {
420 SurfaceCache::CollectSizeOfSurfaces(ImageKey(this), aCounters
, aMallocSizeOf
);
423 nsresult
VectorImage::OnImageDataComplete(nsIRequest
* aRequest
,
424 nsISupports
* aContext
,
425 nsresult aStatus
, bool aLastPart
) {
426 // Call our internal OnStopRequest method, which only talks to our embedded
427 // SVG document. This won't have any effect on our ProgressTracker.
428 nsresult finalStatus
= OnStopRequest(aRequest
, aStatus
);
430 // Give precedence to Necko failure codes.
431 if (NS_FAILED(aStatus
)) {
432 finalStatus
= aStatus
;
435 Progress loadProgress
= LoadCompleteProgress(aLastPart
, mError
, finalStatus
);
437 if (mIsFullyLoaded
|| mError
) {
438 // Our document is loaded, so we're ready to notify now.
439 mProgressTracker
->SyncNotifyProgress(loadProgress
);
441 // Record our progress so far; we'll actually send the notifications in
442 // OnSVGDocumentLoaded or OnSVGDocumentError.
443 mLoadProgress
= Some(loadProgress
);
449 nsresult
VectorImage::OnImageDataAvailable(nsIRequest
* aRequest
,
450 nsISupports
* aContext
,
451 nsIInputStream
* aInStr
,
452 uint64_t aSourceOffset
,
454 return OnDataAvailable(aRequest
, aInStr
, aSourceOffset
, aCount
);
457 nsresult
VectorImage::StartAnimation() {
459 return NS_ERROR_FAILURE
;
462 MOZ_ASSERT(ShouldAnimate(), "Should not animate!");
464 mSVGDocumentWrapper
->StartAnimation();
468 nsresult
VectorImage::StopAnimation() {
471 rv
= NS_ERROR_FAILURE
;
473 MOZ_ASSERT(mIsFullyLoaded
&& mHaveAnimations
,
474 "Should not have been animating!");
476 mSVGDocumentWrapper
->StopAnimation();
483 bool VectorImage::ShouldAnimate() {
484 return ImageResource::ShouldAnimate() && mIsFullyLoaded
&& mHaveAnimations
;
488 VectorImage::SetAnimationStartTime(const TimeStamp
& aTime
) {
489 // We don't care about animation start time.
492 //------------------------------------------------------------------------------
493 // imgIContainer methods
495 //******************************************************************************
497 VectorImage::GetWidth(int32_t* aWidth
) {
498 if (mError
|| !mIsFullyLoaded
) {
499 // XXXdholbert Technically we should leave outparam untouched when we
500 // fail. But since many callers don't check for failure, we set it to 0 on
501 // failure, for sane/predictable results.
503 return NS_ERROR_FAILURE
;
506 SVGSVGElement
* rootElem
= mSVGDocumentWrapper
->GetRootSVGElem();
508 "Should have a root SVG elem, since we finished "
509 "loading without errors");
510 int32_t rootElemWidth
= rootElem
->GetIntrinsicWidth();
511 if (rootElemWidth
< 0) {
513 return NS_ERROR_FAILURE
;
515 *aWidth
= rootElemWidth
;
519 //******************************************************************************
520 nsresult
VectorImage::GetNativeSizes(nsTArray
<IntSize
>& aNativeSizes
) const {
521 return NS_ERROR_NOT_IMPLEMENTED
;
524 //******************************************************************************
525 size_t VectorImage::GetNativeSizesLength() const { return 0; }
527 //******************************************************************************
529 VectorImage::RequestRefresh(const TimeStamp
& aTime
) {
530 if (HadRecentRefresh(aTime
)) {
534 PendingAnimationTracker
* tracker
=
535 mSVGDocumentWrapper
->GetDocument()->GetPendingAnimationTracker();
536 if (tracker
&& ShouldAnimate()) {
537 tracker
->TriggerPendingAnimationsOnNextTick(aTime
);
542 mSVGDocumentWrapper
->TickRefreshDriver();
544 if (mHasPendingInvalidation
) {
545 SendInvalidationNotifications();
549 void VectorImage::SendInvalidationNotifications() {
550 // Animated images don't send out invalidation notifications as soon as
551 // they're generated. Instead, InvalidateObserversOnNextRefreshDriverTick
552 // records that there are pending invalidations and then returns immediately.
553 // The notifications are actually sent from RequestRefresh(). We send these
554 // notifications there to ensure that there is actually a document observing
555 // us. Otherwise, the notifications are just wasted effort.
557 // Non-animated images post an event to call this method from
558 // InvalidateObserversOnNextRefreshDriverTick, because RequestRefresh is never
559 // called for them. Ordinarily this isn't needed, since we send out
560 // invalidation notifications in OnSVGDocumentLoaded, but in rare cases the
561 // SVG document may not be 100% ready to render at that time. In those cases
562 // we would miss the subsequent invalidations if we didn't send out the
563 // notifications indirectly in |InvalidateObservers...|.
565 mHasPendingInvalidation
= false;
566 SurfaceCache::RemoveImage(ImageKey(this));
568 if (UpdateImageContainer(Nothing())) {
569 // If we have image containers, that means we probably won't get a Draw call
570 // from the owner since they are using the container. We must assume all
571 // invalidations need to be handled.
572 MOZ_ASSERT(mRenderingObserver
, "Should have a rendering observer by now");
573 mRenderingObserver
->ResumeHonoringInvalidations();
576 if (mProgressTracker
) {
577 mProgressTracker
->SyncNotifyProgress(FLAG_FRAME_COMPLETE
,
578 GetMaxSizedIntRect());
582 NS_IMETHODIMP_(IntRect
)
583 VectorImage::GetImageSpaceInvalidationRect(const IntRect
& aRect
) {
587 //******************************************************************************
589 VectorImage::GetHeight(int32_t* aHeight
) {
590 if (mError
|| !mIsFullyLoaded
) {
591 // XXXdholbert Technically we should leave outparam untouched when we
592 // fail. But since many callers don't check for failure, we set it to 0 on
593 // failure, for sane/predictable results.
595 return NS_ERROR_FAILURE
;
598 SVGSVGElement
* rootElem
= mSVGDocumentWrapper
->GetRootSVGElem();
600 "Should have a root SVG elem, since we finished "
601 "loading without errors");
602 int32_t rootElemHeight
= rootElem
->GetIntrinsicHeight();
603 if (rootElemHeight
< 0) {
605 return NS_ERROR_FAILURE
;
607 *aHeight
= rootElemHeight
;
611 //******************************************************************************
613 VectorImage::GetIntrinsicSize(nsSize
* aSize
) {
614 if (mError
|| !mIsFullyLoaded
) {
615 return NS_ERROR_FAILURE
;
618 nsIFrame
* rootFrame
= mSVGDocumentWrapper
->GetRootLayoutFrame();
620 return NS_ERROR_FAILURE
;
623 *aSize
= nsSize(-1, -1);
624 IntrinsicSize rfSize
= rootFrame
->GetIntrinsicSize();
626 aSize
->width
= *rfSize
.width
;
629 aSize
->height
= *rfSize
.height
;
634 //******************************************************************************
635 Maybe
<AspectRatio
> VectorImage::GetIntrinsicRatio() {
636 if (mError
|| !mIsFullyLoaded
) {
640 nsIFrame
* rootFrame
= mSVGDocumentWrapper
->GetRootLayoutFrame();
645 return Some(rootFrame
->GetIntrinsicRatio());
648 NS_IMETHODIMP_(Orientation
)
649 VectorImage::GetOrientation() { return Orientation(); }
652 VectorImage::HandledOrientation() { return false; }
654 //******************************************************************************
656 VectorImage::GetType(uint16_t* aType
) {
657 NS_ENSURE_ARG_POINTER(aType
);
659 *aType
= imgIContainer::TYPE_VECTOR
;
663 //******************************************************************************
665 VectorImage::GetProducerId(uint32_t* aId
) {
666 NS_ENSURE_ARG_POINTER(aId
);
668 *aId
= ImageResource::GetImageProducerId();
672 //******************************************************************************
674 VectorImage::GetAnimated(bool* aAnimated
) {
675 if (mError
|| !mIsFullyLoaded
) {
676 return NS_ERROR_FAILURE
;
679 *aAnimated
= mSVGDocumentWrapper
->IsAnimated();
683 //******************************************************************************
684 int32_t VectorImage::GetFirstFrameDelay() {
689 if (!mSVGDocumentWrapper
->IsAnimated()) {
693 // We don't really have a frame delay, so just pretend that we constantly
699 VectorImage::WillDrawOpaqueNow() {
700 return false; // In general, SVG content is not opaque.
703 //******************************************************************************
704 NS_IMETHODIMP_(already_AddRefed
<SourceSurface
>)
705 VectorImage::GetFrame(uint32_t aWhichFrame
, uint32_t aFlags
) {
710 // Look up height & width
711 // ----------------------
712 SVGSVGElement
* svgElem
= mSVGDocumentWrapper
->GetRootSVGElem();
714 "Should have a root SVG elem, since we finished "
715 "loading without errors");
716 nsIntSize
imageIntSize(svgElem
->GetIntrinsicWidth(),
717 svgElem
->GetIntrinsicHeight());
719 if (imageIntSize
.IsEmpty()) {
720 // We'll get here if our SVG doc has a percent-valued or negative width or
725 return GetFrameAtSize(imageIntSize
, aWhichFrame
, aFlags
);
728 NS_IMETHODIMP_(already_AddRefed
<SourceSurface
>)
729 VectorImage::GetFrameAtSize(const IntSize
& aSize
, uint32_t aWhichFrame
,
732 NotifyDrawingObservers();
735 auto result
= GetFrameInternal(aSize
, Nothing(), aWhichFrame
, aFlags
);
736 return Get
<2>(result
).forget();
739 Tuple
<ImgDrawResult
, IntSize
, RefPtr
<SourceSurface
>>
740 VectorImage::GetFrameInternal(const IntSize
& aSize
,
741 const Maybe
<SVGImageContext
>& aSVGContext
,
742 uint32_t aWhichFrame
, uint32_t aFlags
) {
743 MOZ_ASSERT(aWhichFrame
<= FRAME_MAX_VALUE
);
745 if (aSize
.IsEmpty() || aWhichFrame
> FRAME_MAX_VALUE
) {
746 return MakeTuple(ImgDrawResult::BAD_ARGS
, aSize
, RefPtr
<SourceSurface
>());
750 return MakeTuple(ImgDrawResult::BAD_IMAGE
, aSize
, RefPtr
<SourceSurface
>());
753 if (!mIsFullyLoaded
) {
754 return MakeTuple(ImgDrawResult::NOT_READY
, aSize
, RefPtr
<SourceSurface
>());
757 RefPtr
<SourceSurface
> sourceSurface
;
759 Tie(sourceSurface
, decodeSize
) =
760 LookupCachedSurface(aSize
, aSVGContext
, aFlags
);
762 return MakeTuple(ImgDrawResult::SUCCESS
, decodeSize
,
763 std::move(sourceSurface
));
767 NS_WARNING("Refusing to make re-entrant call to VectorImage::Draw");
768 return MakeTuple(ImgDrawResult::TEMPORARY_ERROR
, decodeSize
,
769 RefPtr
<SourceSurface
>());
772 // By using a null gfxContext, we ensure that we will always attempt to
773 // create a surface, even if we aren't capable of caching it (e.g. due to our
774 // flags, having an animation, etc). Otherwise CreateSurface will assume that
775 // the caller is capable of drawing directly to its own draw target if we
777 SVGDrawingParameters
params(
778 nullptr, decodeSize
, aSize
, ImageRegion::Create(decodeSize
),
779 SamplingFilter::POINT
, aSVGContext
,
780 mSVGDocumentWrapper
->GetCurrentTimeAsFloat(), aFlags
, 1.0);
782 bool didCache
; // Was the surface put into the cache?
783 bool contextPaint
= aSVGContext
&& aSVGContext
->GetContextPaint();
785 AutoRestoreSVGState
autoRestore(params
, mSVGDocumentWrapper
, mIsDrawing
,
788 RefPtr
<gfxDrawable
> svgDrawable
= CreateSVGDrawable(params
);
789 RefPtr
<SourceSurface
> surface
= CreateSurface(params
, svgDrawable
, didCache
);
791 MOZ_ASSERT(!didCache
);
792 return MakeTuple(ImgDrawResult::TEMPORARY_ERROR
, decodeSize
,
793 RefPtr
<SourceSurface
>());
796 SendFrameComplete(didCache
, params
.flags
);
797 return MakeTuple(ImgDrawResult::SUCCESS
, decodeSize
, std::move(surface
));
800 //******************************************************************************
801 Tuple
<ImgDrawResult
, IntSize
> VectorImage::GetImageContainerSize(
802 LayerManager
* aManager
, const IntSize
& aSize
, uint32_t aFlags
) {
804 return MakeTuple(ImgDrawResult::BAD_IMAGE
, IntSize(0, 0));
807 if (!mIsFullyLoaded
) {
808 return MakeTuple(ImgDrawResult::NOT_READY
, IntSize(0, 0));
811 if (mHaveAnimations
||
812 aManager
->GetBackendType() != LayersBackend::LAYERS_WR
) {
813 return MakeTuple(ImgDrawResult::NOT_SUPPORTED
, IntSize(0, 0));
816 // We don't need to check if the size is too big since we only support
817 // WebRender backends.
818 if (aSize
.IsEmpty()) {
819 return MakeTuple(ImgDrawResult::BAD_ARGS
, IntSize(0, 0));
822 return MakeTuple(ImgDrawResult::SUCCESS
, aSize
);
826 VectorImage::IsImageContainerAvailable(LayerManager
* aManager
,
828 if (mError
|| !mIsFullyLoaded
|| mHaveAnimations
||
829 aManager
->GetBackendType() != LayersBackend::LAYERS_WR
) {
836 //******************************************************************************
837 NS_IMETHODIMP_(already_AddRefed
<ImageContainer
>)
838 VectorImage::GetImageContainer(LayerManager
* aManager
, uint32_t aFlags
) {
839 MOZ_ASSERT(aManager
->GetBackendType() != LayersBackend::LAYERS_WR
,
840 "WebRender should always use GetImageContainerAvailableAtSize!");
844 //******************************************************************************
846 VectorImage::IsImageContainerAvailableAtSize(LayerManager
* aManager
,
847 const IntSize
& aSize
,
849 // Since we only support image containers with WebRender, and it can handle
850 // textures larger than the hw max texture size, we don't need to check aSize.
851 return !aSize
.IsEmpty() && IsImageContainerAvailable(aManager
, aFlags
);
854 //******************************************************************************
855 NS_IMETHODIMP_(ImgDrawResult
)
856 VectorImage::GetImageContainerAtSize(layers::LayerManager
* aManager
,
857 const gfx::IntSize
& aSize
,
858 const Maybe
<SVGImageContext
>& aSVGContext
,
860 layers::ImageContainer
** aOutContainer
) {
861 Maybe
<SVGImageContext
> newSVGContext
;
862 MaybeRestrictSVGContext(newSVGContext
, aSVGContext
, aFlags
);
864 // The aspect ratio flag was already handled as part of the SVG context
865 // restriction above.
866 uint32_t flags
= aFlags
& ~(FLAG_FORCE_PRESERVEASPECTRATIO_NONE
);
867 return GetImageContainerImpl(aManager
, aSize
,
868 newSVGContext
? newSVGContext
: aSVGContext
,
869 flags
, aOutContainer
);
872 bool VectorImage::MaybeRestrictSVGContext(
873 Maybe
<SVGImageContext
>& aNewSVGContext
,
874 const Maybe
<SVGImageContext
>& aSVGContext
, uint32_t aFlags
) {
876 (aFlags
& FLAG_FORCE_PRESERVEASPECTRATIO_NONE
) && aSVGContext
;
878 bool haveContextPaint
= aSVGContext
&& aSVGContext
->GetContextPaint();
879 bool blockContextPaint
= false;
880 if (haveContextPaint
) {
881 blockContextPaint
= !SVGContextPaint::IsAllowedForImageFromURI(mURI
);
884 if (overridePAR
|| blockContextPaint
) {
885 // The key that we create for the image surface cache must match the way
886 // that the image will be painted, so we need to initialize a new matching
887 // SVGImageContext here in order to generate the correct key.
889 aNewSVGContext
= aSVGContext
; // copy
892 // The SVGImageContext must take account of the preserveAspectRatio
894 MOZ_ASSERT(!aSVGContext
->GetPreserveAspectRatio(),
895 "FLAG_FORCE_PRESERVEASPECTRATIO_NONE is not expected if a "
896 "preserveAspectRatio override is supplied");
897 Maybe
<SVGPreserveAspectRatio
> aspectRatio
= Some(SVGPreserveAspectRatio(
898 SVG_PRESERVEASPECTRATIO_NONE
, SVG_MEETORSLICE_UNKNOWN
));
899 aNewSVGContext
->SetPreserveAspectRatio(aspectRatio
);
902 if (blockContextPaint
) {
903 // The SVGImageContext must not include context paint if the image is
904 // not allowed to use it:
905 aNewSVGContext
->ClearContextPaint();
909 return haveContextPaint
&& !blockContextPaint
;
912 //******************************************************************************
913 NS_IMETHODIMP_(ImgDrawResult
)
914 VectorImage::Draw(gfxContext
* aContext
, const nsIntSize
& aSize
,
915 const ImageRegion
& aRegion
, uint32_t aWhichFrame
,
916 SamplingFilter aSamplingFilter
,
917 const Maybe
<SVGImageContext
>& aSVGContext
, uint32_t aFlags
,
919 if (aWhichFrame
> FRAME_MAX_VALUE
) {
920 return ImgDrawResult::BAD_ARGS
;
924 return ImgDrawResult::BAD_ARGS
;
928 return ImgDrawResult::BAD_IMAGE
;
931 if (!mIsFullyLoaded
) {
932 return ImgDrawResult::NOT_READY
;
935 if (mAnimationConsumers
== 0) {
936 SendOnUnlockedDraw(aFlags
);
939 // We should bypass the cache when:
940 // - We are using a DrawTargetRecording because we prefer the drawing commands
941 // in general to the rasterized surface. This allows blob images to avoid
942 // rasterized SVGs with WebRender.
943 // - The size exceeds what we are willing to cache as a rasterized surface.
944 // We don't do this for WebRender because the performance of the fallback
945 // path is quite bad and upscaling the SVG from the clamped size is better
946 // than bringing the browser to a crawl.
947 if (aContext
->GetDrawTarget()->GetBackendType() == BackendType::RECORDING
||
948 (!gfxVars::UseWebRender() &&
949 aSize
!= SurfaceCache::ClampVectorSize(aSize
))) {
950 aFlags
|= FLAG_BYPASS_SURFACE_CACHE
;
953 MOZ_ASSERT(!(aFlags
& FLAG_FORCE_PRESERVEASPECTRATIO_NONE
) ||
954 (aSVGContext
&& aSVGContext
->GetViewportSize()),
955 "Viewport size is required when using "
956 "FLAG_FORCE_PRESERVEASPECTRATIO_NONE");
958 float animTime
= (aWhichFrame
== FRAME_FIRST
)
960 : mSVGDocumentWrapper
->GetCurrentTimeAsFloat();
962 Maybe
<SVGImageContext
> newSVGContext
;
964 MaybeRestrictSVGContext(newSVGContext
, aSVGContext
, aFlags
);
966 SVGDrawingParameters
params(aContext
, aSize
, aSize
, aRegion
, aSamplingFilter
,
967 newSVGContext
? newSVGContext
: aSVGContext
,
968 animTime
, aFlags
, aOpacity
);
970 // If we have an prerasterized version of this image that matches the
971 // drawing parameters, use that.
972 RefPtr
<SourceSurface
> sourceSurface
;
973 Tie(sourceSurface
, params
.size
) =
974 LookupCachedSurface(aSize
, params
.svgContext
, aFlags
);
976 RefPtr
<gfxDrawable
> drawable
=
977 new gfxSurfaceDrawable(sourceSurface
, params
.size
);
978 Show(drawable
, params
);
979 return ImgDrawResult::SUCCESS
;
982 // else, we need to paint the image:
985 NS_WARNING("Refusing to make re-entrant call to VectorImage::Draw");
986 return ImgDrawResult::TEMPORARY_ERROR
;
989 AutoRestoreSVGState
autoRestore(params
, mSVGDocumentWrapper
, mIsDrawing
,
992 bool didCache
; // Was the surface put into the cache?
993 RefPtr
<gfxDrawable
> svgDrawable
= CreateSVGDrawable(params
);
994 sourceSurface
= CreateSurface(params
, svgDrawable
, didCache
);
995 if (!sourceSurface
) {
996 MOZ_ASSERT(!didCache
);
997 Show(svgDrawable
, params
);
998 return ImgDrawResult::SUCCESS
;
1001 RefPtr
<gfxDrawable
> drawable
=
1002 new gfxSurfaceDrawable(sourceSurface
, params
.size
);
1003 Show(drawable
, params
);
1004 SendFrameComplete(didCache
, params
.flags
);
1005 return ImgDrawResult::SUCCESS
;
1008 already_AddRefed
<gfxDrawable
> VectorImage::CreateSVGDrawable(
1009 const SVGDrawingParameters
& aParams
) {
1010 RefPtr
<gfxDrawingCallback
> cb
= new SVGDrawingCallback(
1011 mSVGDocumentWrapper
, aParams
.viewportSize
, aParams
.size
, aParams
.flags
);
1013 RefPtr
<gfxDrawable
> svgDrawable
= new gfxCallbackDrawable(cb
, aParams
.size
);
1014 return svgDrawable
.forget();
1017 Tuple
<RefPtr
<SourceSurface
>, IntSize
> VectorImage::LookupCachedSurface(
1018 const IntSize
& aSize
, const Maybe
<SVGImageContext
>& aSVGContext
,
1020 // If we're not allowed to use a cached surface, don't attempt a lookup.
1021 if (aFlags
& FLAG_BYPASS_SURFACE_CACHE
) {
1022 return MakeTuple(RefPtr
<SourceSurface
>(), aSize
);
1025 // We don't do any caching if we have animation, so don't bother with a lookup
1026 // in this case either.
1027 if (mHaveAnimations
) {
1028 return MakeTuple(RefPtr
<SourceSurface
>(), aSize
);
1031 LookupResult
result(MatchType::NOT_FOUND
);
1032 SurfaceKey surfaceKey
= VectorSurfaceKey(aSize
, aSVGContext
);
1033 if ((aFlags
& FLAG_SYNC_DECODE
) || !(aFlags
& FLAG_HIGH_QUALITY_SCALING
)) {
1034 result
= SurfaceCache::Lookup(ImageKey(this), surfaceKey
,
1035 /* aMarkUsed = */ true);
1037 result
= SurfaceCache::LookupBestMatch(ImageKey(this), surfaceKey
,
1038 /* aMarkUsed = */ true);
1041 IntSize rasterSize
=
1042 result
.SuggestedSize().IsEmpty() ? aSize
: result
.SuggestedSize();
1043 MOZ_ASSERT(result
.Type() != MatchType::SUBSTITUTE_BECAUSE_PENDING
);
1044 if (!result
|| result
.Type() == MatchType::SUBSTITUTE_BECAUSE_NOT_FOUND
) {
1045 // No matching surface, or the OS freed the volatile buffer.
1046 return MakeTuple(RefPtr
<SourceSurface
>(), rasterSize
);
1049 RefPtr
<SourceSurface
> sourceSurface
= result
.Surface()->GetSourceSurface();
1050 if (!sourceSurface
) {
1051 // Something went wrong. (Probably a GPU driver crash or device reset.)
1052 // Attempt to recover.
1053 RecoverFromLossOfSurfaces();
1054 return MakeTuple(RefPtr
<SourceSurface
>(), rasterSize
);
1057 return MakeTuple(std::move(sourceSurface
), rasterSize
);
1060 already_AddRefed
<SourceSurface
> VectorImage::CreateSurface(
1061 const SVGDrawingParameters
& aParams
, gfxDrawable
* aSVGDrawable
,
1063 MOZ_ASSERT(mIsDrawing
);
1065 mSVGDocumentWrapper
->UpdateViewportBounds(aParams
.viewportSize
);
1066 mSVGDocumentWrapper
->FlushImageTransformInvalidation();
1068 // Determine whether or not we should put the surface to be created into
1069 // the cache. If we fail, we need to reset this to false to let the caller
1070 // know nothing was put in the cache.
1071 aWillCache
= !(aParams
.flags
& FLAG_BYPASS_SURFACE_CACHE
) &&
1072 // Refuse to cache animated images:
1073 // XXX(seth): We may remove this restriction in bug 922893.
1075 // The image is too big to fit in the cache:
1076 SurfaceCache::CanHold(aParams
.size
);
1078 // If we weren't given a context, then we know we just want the rasterized
1079 // surface. We will create the frame below but only insert it into the cache
1080 // if we actually need to.
1081 if (!aWillCache
&& aParams
.context
) {
1085 // We're about to rerasterize, which may mean that some of the previous
1086 // surfaces we've rasterized aren't useful anymore. We can allow them to
1087 // expire from the cache by unlocking them here, and then sending out an
1088 // invalidation. If this image is locked, any surfaces that are still useful
1089 // will become locked again when Draw touches them, and the remainder will
1090 // eventually expire.
1092 SurfaceCache::UnlockEntries(ImageKey(this));
1095 // If there is no context, the default backend is fine.
1096 BackendType backend
=
1097 aParams
.context
? aParams
.context
->GetDrawTarget()->GetBackendType()
1098 : gfxPlatform::GetPlatform()->GetDefaultContentBackend();
1100 // Try to create an imgFrame, initializing the surface it contains by drawing
1101 // our gfxDrawable into it. (We use FILTER_NEAREST since we never scale here.)
1102 auto frame
= MakeNotNull
<RefPtr
<imgFrame
>>();
1103 nsresult rv
= frame
->InitWithDrawable(
1104 aSVGDrawable
, aParams
.size
, SurfaceFormat::OS_RGBA
, SamplingFilter::POINT
,
1105 aParams
.flags
, backend
);
1107 // If we couldn't create the frame, it was probably because it would end
1108 // up way too big. Generally it also wouldn't fit in the cache, but the prefs
1109 // could be set such that the cache isn't the limiting factor.
1110 if (NS_FAILED(rv
)) {
1115 // Take a strong reference to the frame's surface and make sure it hasn't
1116 // already been purged by the operating system.
1117 RefPtr
<SourceSurface
> surface
= frame
->GetSourceSurface();
1123 // We created the frame, but only because we had no context to draw to
1124 // directly. All the caller wants is the surface in this case.
1126 return surface
.forget();
1129 // Attempt to cache the frame.
1130 SurfaceKey surfaceKey
= VectorSurfaceKey(aParams
.size
, aParams
.svgContext
);
1131 NotNull
<RefPtr
<ISurfaceProvider
>> provider
=
1132 MakeNotNull
<SimpleSurfaceProvider
*>(ImageKey(this), surfaceKey
, frame
);
1134 if (SurfaceCache::Insert(provider
) == InsertOutcome::SUCCESS
) {
1135 if (aParams
.size
!= aParams
.drawSize
) {
1136 // We created a new surface that wasn't the size we requested, which means
1137 // we entered factor-of-2 mode. We should purge any surfaces we no longer
1138 // need rather than waiting for the cache to expire them.
1139 SurfaceCache::PruneImage(ImageKey(this));
1145 return surface
.forget();
1148 void VectorImage::SendFrameComplete(bool aDidCache
, uint32_t aFlags
) {
1149 // If the cache was not updated, we have nothing to do.
1154 // Send out an invalidation so that surfaces that are still in use get
1155 // re-locked. See the discussion of the UnlockSurfaces call above.
1156 if (!(aFlags
& FLAG_ASYNC_NOTIFY
)) {
1157 mProgressTracker
->SyncNotifyProgress(FLAG_FRAME_COMPLETE
,
1158 GetMaxSizedIntRect());
1160 NotNull
<RefPtr
<VectorImage
>> image
= WrapNotNull(this);
1161 NS_DispatchToMainThread(CreateMediumHighRunnable(NS_NewRunnableFunction(
1162 "ProgressTracker::SyncNotifyProgress", [=]() -> void {
1163 RefPtr
<ProgressTracker
> tracker
= image
->GetProgressTracker();
1165 tracker
->SyncNotifyProgress(FLAG_FRAME_COMPLETE
,
1166 GetMaxSizedIntRect());
1172 void VectorImage::Show(gfxDrawable
* aDrawable
,
1173 const SVGDrawingParameters
& aParams
) {
1174 // The surface size may differ from the size at which we wish to draw. As
1175 // such, we may need to adjust the context/region to take this into account.
1176 gfxContextMatrixAutoSaveRestore
saveMatrix(aParams
.context
);
1177 ImageRegion
region(aParams
.region
);
1178 if (aParams
.drawSize
!= aParams
.size
) {
1179 gfx::Size
scale(double(aParams
.drawSize
.width
) / aParams
.size
.width
,
1180 double(aParams
.drawSize
.height
) / aParams
.size
.height
);
1181 aParams
.context
->Multiply(gfxMatrix::Scaling(scale
.width
, scale
.height
));
1182 region
.Scale(1.0 / scale
.width
, 1.0 / scale
.height
);
1185 MOZ_ASSERT(aDrawable
, "Should have a gfxDrawable by now");
1186 gfxUtils::DrawPixelSnapped(aParams
.context
, aDrawable
,
1187 SizeDouble(aParams
.size
), region
,
1188 SurfaceFormat::OS_RGBA
, aParams
.samplingFilter
,
1189 aParams
.flags
, aParams
.opacity
, false);
1192 NotifyDrawingObservers();
1195 MOZ_ASSERT(mRenderingObserver
, "Should have a rendering observer by now");
1196 mRenderingObserver
->ResumeHonoringInvalidations();
1199 void VectorImage::RecoverFromLossOfSurfaces() {
1200 NS_WARNING("An imgFrame became invalid. Attempting to recover...");
1202 // Discard all existing frames, since they're probably all now invalid.
1203 SurfaceCache::RemoveImage(ImageKey(this));
1207 VectorImage::StartDecoding(uint32_t aFlags
, uint32_t aWhichFrame
) {
1208 // Nothing to do for SVG images
1212 bool VectorImage::StartDecodingWithResult(uint32_t aFlags
,
1213 uint32_t aWhichFrame
) {
1214 // SVG images are ready to draw when they are loaded
1215 return mIsFullyLoaded
;
1218 imgIContainer::DecodeResult
VectorImage::RequestDecodeWithResult(
1219 uint32_t aFlags
, uint32_t aWhichFrame
) {
1220 // SVG images are ready to draw when they are loaded and don't have an error.
1223 return imgIContainer::DECODE_REQUEST_FAILED
;
1226 if (!mIsFullyLoaded
) {
1227 return imgIContainer::DECODE_REQUESTED
;
1230 return imgIContainer::DECODE_SURFACE_AVAILABLE
;
1234 VectorImage::RequestDecodeForSize(const nsIntSize
& aSize
, uint32_t aFlags
,
1235 uint32_t aWhichFrame
) {
1236 // Nothing to do for SVG images, though in theory we could rasterize to the
1237 // provided size ahead of time if we supported off-main-thread SVG
1242 //******************************************************************************
1245 VectorImage::LockImage() {
1246 MOZ_ASSERT(NS_IsMainThread());
1249 return NS_ERROR_FAILURE
;
1254 if (mLockCount
== 1) {
1255 // Lock this image's surfaces in the SurfaceCache.
1256 SurfaceCache::LockImage(ImageKey(this));
1262 //******************************************************************************
1265 VectorImage::UnlockImage() {
1266 MOZ_ASSERT(NS_IsMainThread());
1269 return NS_ERROR_FAILURE
;
1272 if (mLockCount
== 0) {
1273 MOZ_ASSERT_UNREACHABLE("Calling UnlockImage with a zero lock count");
1274 return NS_ERROR_ABORT
;
1279 if (mLockCount
== 0) {
1280 // Unlock this image's surfaces in the SurfaceCache.
1281 SurfaceCache::UnlockImage(ImageKey(this));
1287 //******************************************************************************
1290 VectorImage::RequestDiscard() {
1291 MOZ_ASSERT(NS_IsMainThread());
1293 if (mDiscardable
&& mLockCount
== 0) {
1294 SurfaceCache::RemoveImage(ImageKey(this));
1295 mProgressTracker
->OnDiscard();
1301 void VectorImage::OnSurfaceDiscarded(const SurfaceKey
& aSurfaceKey
) {
1302 MOZ_ASSERT(mProgressTracker
);
1304 NS_DispatchToMainThread(NewRunnableMethod("ProgressTracker::OnDiscard",
1306 &ProgressTracker::OnDiscard
));
1309 //******************************************************************************
1311 VectorImage::ResetAnimation() {
1313 return NS_ERROR_FAILURE
;
1316 if (!mIsFullyLoaded
|| !mHaveAnimations
) {
1317 return NS_OK
; // There are no animations to be reset.
1320 mSVGDocumentWrapper
->ResetAnimation();
1325 NS_IMETHODIMP_(float)
1326 VectorImage::GetFrameIndex(uint32_t aWhichFrame
) {
1327 MOZ_ASSERT(aWhichFrame
<= FRAME_MAX_VALUE
, "Invalid argument");
1328 return aWhichFrame
== FRAME_FIRST
1330 : mSVGDocumentWrapper
->GetCurrentTimeAsFloat();
1333 //------------------------------------------------------------------------------
1334 // nsIRequestObserver methods
1336 //******************************************************************************
1338 VectorImage::OnStartRequest(nsIRequest
* aRequest
) {
1339 MOZ_ASSERT(!mSVGDocumentWrapper
,
1340 "Repeated call to OnStartRequest -- can this happen?");
1342 mSVGDocumentWrapper
= new SVGDocumentWrapper();
1343 nsresult rv
= mSVGDocumentWrapper
->OnStartRequest(aRequest
);
1344 if (NS_FAILED(rv
)) {
1345 mSVGDocumentWrapper
= nullptr;
1350 // Create a listener to wait until the SVG document is fully loaded, which
1351 // will signal that this image is ready to render. Certain error conditions
1352 // will prevent us from ever getting this notification, so we also create a
1353 // listener that waits for parsing to complete and cancels the
1354 // SVGLoadEventListener if needed. The listeners are automatically attached
1355 // to the document by their constructors.
1356 SVGDocument
* document
= mSVGDocumentWrapper
->GetDocument();
1357 mLoadEventListener
= new SVGLoadEventListener(document
, this);
1358 mParseCompleteListener
= new SVGParseCompleteListener(document
, this);
1360 // Displayed documents will call InitUseCounters under SetScriptGlobalObject,
1361 // but SVG image documents never get a script global object, so we initialize
1362 // use counters here, right after the document has been created.
1363 document
->InitUseCounters();
1368 //******************************************************************************
1370 VectorImage::OnStopRequest(nsIRequest
* aRequest
, nsresult aStatus
) {
1372 return NS_ERROR_FAILURE
;
1375 return mSVGDocumentWrapper
->OnStopRequest(aRequest
, aStatus
);
1378 void VectorImage::OnSVGDocumentParsed() {
1379 MOZ_ASSERT(mParseCompleteListener
, "Should have the parse complete listener");
1380 MOZ_ASSERT(mLoadEventListener
, "Should have the load event listener");
1382 if (!mSVGDocumentWrapper
->GetRootSVGElem()) {
1383 // This is an invalid SVG document. It may have failed to parse, or it may
1384 // be missing the <svg> root element, or the <svg> root element may not
1385 // declare the correct namespace. In any of these cases, we'll never be
1386 // notified that the SVG finished loading, so we need to treat this as an
1388 OnSVGDocumentError();
1392 void VectorImage::CancelAllListeners() {
1393 if (mParseCompleteListener
) {
1394 mParseCompleteListener
->Cancel();
1395 mParseCompleteListener
= nullptr;
1397 if (mLoadEventListener
) {
1398 mLoadEventListener
->Cancel();
1399 mLoadEventListener
= nullptr;
1403 void VectorImage::OnSVGDocumentLoaded() {
1404 MOZ_ASSERT(mSVGDocumentWrapper
->GetRootSVGElem(),
1405 "Should have parsed successfully");
1406 MOZ_ASSERT(!mIsFullyLoaded
&& !mHaveAnimations
,
1407 "These flags shouldn't get set until OnSVGDocumentLoaded. "
1408 "Duplicate calls to OnSVGDocumentLoaded?");
1410 CancelAllListeners();
1412 // XXX Flushing is wasteful if embedding frame hasn't had initial reflow.
1413 mSVGDocumentWrapper
->FlushLayout();
1415 // This is the earliest point that we can get accurate use counter data
1416 // for a valid SVG document. Without the FlushLayout call, we would miss
1417 // any CSS property usage that comes from SVG presentation attributes.
1418 mSVGDocumentWrapper
->GetDocument()->ReportDocumentUseCounters();
1420 mIsFullyLoaded
= true;
1421 mHaveAnimations
= mSVGDocumentWrapper
->IsAnimated();
1423 // Start listening to our image for rendering updates.
1424 mRenderingObserver
= new SVGRootRenderingObserver(mSVGDocumentWrapper
, this);
1426 // ProgressTracker::SyncNotifyProgress may release us, so ensure we
1427 // stick around long enough to complete our work.
1428 RefPtr
<VectorImage
> kungFuDeathGrip(this);
1430 // Tell *our* observers that we're done loading.
1431 if (mProgressTracker
) {
1432 Progress progress
= FLAG_SIZE_AVAILABLE
| FLAG_HAS_TRANSPARENCY
|
1433 FLAG_FRAME_COMPLETE
| FLAG_DECODE_COMPLETE
;
1435 if (mHaveAnimations
) {
1436 progress
|= FLAG_IS_ANIMATED
;
1439 // Merge in any saved progress from OnImageDataComplete.
1440 if (mLoadProgress
) {
1441 progress
|= *mLoadProgress
;
1442 mLoadProgress
= Nothing();
1445 mProgressTracker
->SyncNotifyProgress(progress
, GetMaxSizedIntRect());
1448 EvaluateAnimation();
1451 void VectorImage::OnSVGDocumentError() {
1452 CancelAllListeners();
1456 // We won't enter OnSVGDocumentLoaded, so report use counters now for this
1457 // invalid document.
1458 ReportDocumentUseCounters();
1460 if (mProgressTracker
) {
1461 // Notify observers about the error and unblock page load.
1462 Progress progress
= FLAG_HAS_ERROR
;
1464 // Merge in any saved progress from OnImageDataComplete.
1465 if (mLoadProgress
) {
1466 progress
|= *mLoadProgress
;
1467 mLoadProgress
= Nothing();
1470 mProgressTracker
->SyncNotifyProgress(progress
);
1474 //------------------------------------------------------------------------------
1475 // nsIStreamListener method
1477 //******************************************************************************
1479 VectorImage::OnDataAvailable(nsIRequest
* aRequest
, nsIInputStream
* aInStr
,
1480 uint64_t aSourceOffset
, uint32_t aCount
) {
1482 return NS_ERROR_FAILURE
;
1485 return mSVGDocumentWrapper
->OnDataAvailable(aRequest
, aInStr
, aSourceOffset
,
1489 // --------------------------
1490 // Invalidation helper method
1492 void VectorImage::InvalidateObserversOnNextRefreshDriverTick() {
1493 if (mHasPendingInvalidation
) {
1497 mHasPendingInvalidation
= true;
1499 // Animated images can wait for the refresh tick.
1500 if (mHaveAnimations
) {
1504 // Non-animated images won't get the refresh tick, so we should just send an
1505 // invalidation outside the current execution context. We need to defer
1506 // because the layout tree is in the middle of invalidation, and the tree
1507 // state needs to be consistent. Specifically only some of the frames have
1508 // had the NS_FRAME_DESCENDANT_NEEDS_PAINT and/or NS_FRAME_NEEDS_PAINT bits
1509 // set by InvalidateFrameInternal in layout/generic/nsFrame.cpp. These bits
1510 // get cleared when we repaint the SVG into a surface by
1511 // nsIFrame::ClearInvalidationStateBits in nsDisplayList::PaintRoot.
1512 nsCOMPtr
<nsIEventTarget
> eventTarget
;
1513 if (mProgressTracker
) {
1514 eventTarget
= mProgressTracker
->GetEventTarget();
1516 eventTarget
= do_GetMainThread();
1519 RefPtr
<VectorImage
> self(this);
1520 nsCOMPtr
<nsIRunnable
> ev(NS_NewRunnableFunction(
1521 "VectorImage::SendInvalidationNotifications",
1522 [=]() -> void { self
->SendInvalidationNotifications(); }));
1523 eventTarget
->Dispatch(CreateMediumHighRunnable(ev
.forget()),
1524 NS_DISPATCH_NORMAL
);
1527 void VectorImage::PropagateUseCounters(Document
* aReferencingDocument
) {
1528 if (Document
* doc
= mSVGDocumentWrapper
->GetDocument()) {
1529 doc
->PropagateImageUseCounters(aReferencingDocument
);
1533 nsIntSize
VectorImage::OptimalImageSizeForDest(const gfxSize
& aDest
,
1534 uint32_t aWhichFrame
,
1535 SamplingFilter aSamplingFilter
,
1537 MOZ_ASSERT(aDest
.width
>= 0 || ceil(aDest
.width
) <= INT32_MAX
||
1538 aDest
.height
>= 0 || ceil(aDest
.height
) <= INT32_MAX
,
1539 "Unexpected destination size");
1541 // We can rescale SVGs freely, so just return the provided destination size.
1542 return nsIntSize::Ceil(aDest
.width
, aDest
.height
);
1545 already_AddRefed
<imgIContainer
> VectorImage::Unwrap() {
1546 nsCOMPtr
<imgIContainer
> self(this);
1547 return self
.forget();
1550 void VectorImage::MediaFeatureValuesChangedAllDocuments(
1551 const MediaFeatureChange
& aChange
) {
1552 if (!mSVGDocumentWrapper
) {
1556 // Don't bother if the document hasn't loaded yet.
1557 if (!mIsFullyLoaded
) {
1561 if (Document
* doc
= mSVGDocumentWrapper
->GetDocument()) {
1562 if (RefPtr
<nsPresContext
> presContext
= doc
->GetPresContext()) {
1563 presContext
->MediaFeatureValuesChanged(
1564 aChange
, MediaFeatureChangePropagation::All
);
1565 // Media feature value changes don't happen in the middle of layout,
1566 // so we don't need to call InvalidateObserversOnNextRefreshDriverTick
1567 // to invalidate asynchronously.
1568 if (presContext
->FlushPendingMediaFeatureValuesChanged()) {
1569 // NOTE(emilio): SendInvalidationNotifications flushes layout via
1570 // VectorImage::CreateSurface -> FlushImageTransformInvalidation.
1571 SendInvalidationNotifications();
1577 nsresult
VectorImage::GetHotspotX(int32_t* aX
) {
1578 return Image::GetHotspotX(aX
);
1581 nsresult
VectorImage::GetHotspotY(int32_t* aY
) {
1582 return Image::GetHotspotY(aY
);
1585 void VectorImage::ReportDocumentUseCounters() {
1586 if (!mSVGDocumentWrapper
) {
1590 if (Document
* doc
= mSVGDocumentWrapper
->GetDocument()) {
1591 doc
->ReportDocumentUseCounters();
1595 } // namespace image
1596 } // namespace mozilla