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 // Must #include ImageLogging.h before any IPDL-generated files or other files
7 // that #include prlog.h
8 #include "ImageLogging.h"
10 #include "RasterImage.h"
12 #include "gfxPlatform.h"
13 #include "nsComponentManagerUtils.h"
15 #include "DecodePool.h"
19 #include "IDecodingTask.h"
20 #include "ImageRegion.h"
22 #include "LookupResult.h"
23 #include "nsIConsoleService.h"
24 #include "nsIInputStream.h"
25 #include "nsIScriptError.h"
26 #include "nsISupportsPrimitives.h"
28 #include "nsPresContext.h"
29 #include "SourceBuffer.h"
30 #include "SurfaceCache.h"
31 #include "FrameAnimator.h"
33 #include "gfxContext.h"
35 #include "mozilla/gfx/2D.h"
36 #include "mozilla/DebugOnly.h"
37 #include "mozilla/Likely.h"
38 #include "mozilla/RefPtr.h"
39 #include "mozilla/Move.h"
40 #include "mozilla/MemoryReporting.h"
41 #include "mozilla/SizeOfState.h"
43 #include "mozilla/Telemetry.h"
44 #include "mozilla/TimeStamp.h"
45 #include "mozilla/Tuple.h"
46 #include "mozilla/ClearOnShutdown.h"
47 #include "mozilla/gfx/Scale.h"
49 #include "GeckoProfiler.h"
50 #include "gfx2DGlue.h"
52 #include "nsProperties.h"
58 using namespace layers
;
66 NS_IMPL_ISUPPORTS(RasterImage
, imgIContainer
, nsIProperties
)
68 NS_IMPL_ISUPPORTS(RasterImage
, imgIContainer
, nsIProperties
, imgIContainerDebug
)
71 //******************************************************************************
72 RasterImage::RasterImage(nsIURI
* aURI
/* = nullptr */)
73 : ImageResource(aURI
), // invoke superclass's constructor
76 mDecoderType(DecoderType::UNKNOWN
),
81 mSourceBuffer(MakeNotNull
<SourceBuffer
*>()),
86 mSomeSourceData(false),
87 mAllSourceData(false),
88 mHasBeenDecoded(false),
89 mPendingAnimation(false),
90 mAnimationFinished(false),
91 mWantFullDecode(false) {
94 //******************************************************************************
95 RasterImage::~RasterImage() {
96 // Make sure our SourceBuffer is marked as complete. This will ensure that any
97 // outstanding decoders terminate.
98 if (!mSourceBuffer
->IsComplete()) {
99 mSourceBuffer
->Complete(NS_ERROR_ABORT
);
102 // Release all frames from the surface cache.
103 SurfaceCache::RemoveImage(ImageKey(this));
106 Telemetry::Accumulate(Telemetry::IMAGE_DECODE_COUNT
, mDecodeCount
);
109 nsresult
RasterImage::Init(const char* aMimeType
, uint32_t aFlags
) {
110 // We don't support re-initialization
112 return NS_ERROR_ILLEGAL_VALUE
;
115 // Not sure an error can happen before init, but be safe
117 return NS_ERROR_FAILURE
;
120 // We want to avoid redecodes for transient images.
121 MOZ_ASSERT_IF(aFlags
& INIT_FLAG_TRANSIENT
,
122 !(aFlags
& INIT_FLAG_DISCARDABLE
));
124 // Store initialization data
125 mDiscardable
= !!(aFlags
& INIT_FLAG_DISCARDABLE
);
126 mWantFullDecode
= !!(aFlags
& INIT_FLAG_DECODE_IMMEDIATELY
);
127 mTransient
= !!(aFlags
& INIT_FLAG_TRANSIENT
);
128 mSyncLoad
= !!(aFlags
& INIT_FLAG_SYNC_LOAD
);
130 // Use the MIME type to select a decoder type, and make sure there *is* a
131 // decoder for this MIME type.
132 NS_ENSURE_ARG_POINTER(aMimeType
);
133 mDecoderType
= DecoderFactory::GetDecoderType(aMimeType
);
134 if (mDecoderType
== DecoderType::UNKNOWN
) {
135 return NS_ERROR_FAILURE
;
138 // Lock this image's surfaces in the SurfaceCache if we're not discardable.
141 SurfaceCache::LockImage(ImageKey(this));
144 // Mark us as initialized
150 //******************************************************************************
152 RasterImage::RequestRefresh(const TimeStamp
& aTime
) {
153 if (HadRecentRefresh(aTime
)) {
164 if (mAnimationState
) {
165 MOZ_ASSERT(mFrameAnimator
);
166 res
= mFrameAnimator
->RequestRefresh(*mAnimationState
, aTime
,
170 if (res
.mFrameAdvanced
) {
171 // Notify listeners that our frame has actually changed, but do this only
172 // once for all frames that we've now passed (if AdvanceFrame() was called
178 NotifyProgress(NoProgress
, res
.mDirtyRect
);
181 if (res
.mAnimationFinished
) {
182 mAnimationFinished
= true;
187 //******************************************************************************
189 RasterImage::GetWidth(int32_t* aWidth
) {
190 NS_ENSURE_ARG_POINTER(aWidth
);
194 return NS_ERROR_FAILURE
;
197 *aWidth
= mSize
.width
;
201 //******************************************************************************
203 RasterImage::GetHeight(int32_t* aHeight
) {
204 NS_ENSURE_ARG_POINTER(aHeight
);
208 return NS_ERROR_FAILURE
;
211 *aHeight
= mSize
.height
;
215 //******************************************************************************
216 nsresult
RasterImage::GetNativeSizes(nsTArray
<IntSize
>& aNativeSizes
) const {
218 return NS_ERROR_FAILURE
;
221 if (mNativeSizes
.IsEmpty()) {
222 aNativeSizes
.Clear();
223 aNativeSizes
.AppendElement(mSize
);
225 aNativeSizes
= mNativeSizes
;
231 //******************************************************************************
232 size_t RasterImage::GetNativeSizesLength() const {
233 if (mError
|| !mHasSize
) {
237 if (mNativeSizes
.IsEmpty()) {
241 return mNativeSizes
.Length();
244 //******************************************************************************
246 RasterImage::GetIntrinsicSize(nsSize
* aSize
) {
248 return NS_ERROR_FAILURE
;
251 *aSize
= nsSize(nsPresContext::CSSPixelsToAppUnits(mSize
.width
),
252 nsPresContext::CSSPixelsToAppUnits(mSize
.height
));
256 //******************************************************************************
258 RasterImage::GetIntrinsicRatio(nsSize
* aRatio
) {
260 return NS_ERROR_FAILURE
;
263 *aRatio
= nsSize(mSize
.width
, mSize
.height
);
267 NS_IMETHODIMP_(Orientation
)
268 RasterImage::GetOrientation() { return mOrientation
; }
270 //******************************************************************************
272 RasterImage::GetType(uint16_t* aType
) {
273 NS_ENSURE_ARG_POINTER(aType
);
275 *aType
= imgIContainer::TYPE_RASTER
;
279 LookupResult
RasterImage::LookupFrameInternal(const IntSize
& aSize
,
281 PlaybackType aPlaybackType
,
283 if (mAnimationState
&& aPlaybackType
== PlaybackType::eAnimated
) {
284 MOZ_ASSERT(mFrameAnimator
);
285 MOZ_ASSERT(ToSurfaceFlags(aFlags
) == DefaultSurfaceFlags(),
286 "Can't composite frames with non-default surface flags");
287 return mFrameAnimator
->GetCompositedFrame(*mAnimationState
, aMarkUsed
);
290 SurfaceFlags surfaceFlags
= ToSurfaceFlags(aFlags
);
292 // We don't want any substitution for sync decodes, and substitution would be
293 // illegal when high quality downscaling is disabled, so we use
294 // SurfaceCache::Lookup in this case.
295 if ((aFlags
& FLAG_SYNC_DECODE
) || !(aFlags
& FLAG_HIGH_QUALITY_SCALING
)) {
296 return SurfaceCache::Lookup(
298 RasterSurfaceKey(aSize
, surfaceFlags
, PlaybackType::eStatic
),
302 // We'll return the best match we can find to the requested frame.
303 return SurfaceCache::LookupBestMatch(
305 RasterSurfaceKey(aSize
, surfaceFlags
, PlaybackType::eStatic
), aMarkUsed
);
308 LookupResult
RasterImage::LookupFrame(const IntSize
& aSize
, uint32_t aFlags
,
309 PlaybackType aPlaybackType
,
311 MOZ_ASSERT(NS_IsMainThread());
313 // If we're opaque, we don't need to care about premultiplied alpha, because
314 // that can only matter for frames with transparency.
316 aFlags
&= ~FLAG_DECODE_NO_PREMULTIPLY_ALPHA
;
319 IntSize requestedSize
=
320 CanDownscaleDuringDecode(aSize
, aFlags
) ? aSize
: mSize
;
321 if (requestedSize
.IsEmpty()) {
322 // Can't decode to a surface of zero size.
323 return LookupResult(MatchType::NOT_FOUND
);
326 LookupResult result
=
327 LookupFrameInternal(requestedSize
, aFlags
, aPlaybackType
, aMarkUsed
);
329 if (!result
&& !mHasSize
) {
330 // We can't request a decode without knowing our intrinsic size. Give up.
331 return LookupResult(MatchType::NOT_FOUND
);
334 if (result
.Type() == MatchType::NOT_FOUND
||
335 result
.Type() == MatchType::SUBSTITUTE_BECAUSE_NOT_FOUND
||
336 ((aFlags
& FLAG_SYNC_DECODE
) && !result
)) {
337 // We don't have a copy of this frame, and there's no decoder working on
338 // one. (Or we're sync decoding and the existing decoder hasn't even started
339 // yet.) Trigger decoding so it'll be available next time.
340 MOZ_ASSERT(aPlaybackType
!= PlaybackType::eAnimated
||
341 gfxPrefs::ImageMemAnimatedDiscardable() ||
342 !mAnimationState
|| mAnimationState
->KnownFrameCount() < 1,
343 "Animated frames should be locked");
345 // The surface cache may suggest the preferred size we are supposed to
346 // decode at. This should only happen if we accept substitutions.
347 if (!result
.SuggestedSize().IsEmpty()) {
348 MOZ_ASSERT(!(aFlags
& FLAG_SYNC_DECODE
) &&
349 (aFlags
& FLAG_HIGH_QUALITY_SCALING
));
350 requestedSize
= result
.SuggestedSize();
353 bool ranSync
= Decode(requestedSize
, aFlags
, aPlaybackType
);
355 // If we can or did sync decode, we should already have the frame.
356 if (ranSync
|| (aFlags
& FLAG_SYNC_DECODE
)) {
358 LookupFrameInternal(requestedSize
, aFlags
, aPlaybackType
, aMarkUsed
);
363 // We still weren't able to get a frame. Give up.
367 if (result
.Surface()->GetCompositingFailed()) {
368 DrawableSurface tmp
= std::move(result
.Surface());
372 MOZ_ASSERT(!result
.Surface()->GetIsPaletted(),
373 "Should not have a paletted frame");
375 // Sync decoding guarantees that we got the frame, but if it's owned by an
376 // async decoder that's currently running, the contents of the frame may not
377 // be available yet. Make sure we get everything.
378 if (mAllSourceData
&& (aFlags
& FLAG_SYNC_DECODE
)) {
379 result
.Surface()->WaitUntilFinished();
382 // If we could have done some decoding in this function we need to check if
383 // that decoding encountered an error and hence aborted the surface. We want
384 // to avoid calling IsAborted if we weren't passed any sync decode flag
385 // because IsAborted acquires the monitor for the imgFrame.
386 if (aFlags
& (FLAG_SYNC_DECODE
| FLAG_SYNC_DECODE_IF_FAST
) &&
387 result
.Surface()->IsAborted()) {
388 DrawableSurface tmp
= std::move(result
.Surface());
395 bool RasterImage::IsOpaque() {
400 Progress progress
= mProgressTracker
->GetProgress();
402 // If we haven't yet finished decoding, the safe answer is "not opaque".
403 if (!(progress
& FLAG_DECODE_COMPLETE
)) {
407 // Other, we're opaque if FLAG_HAS_TRANSPARENCY is not set.
408 return !(progress
& FLAG_HAS_TRANSPARENCY
);
412 RasterImage::WillDrawOpaqueNow() {
417 if (mAnimationState
) {
418 if (!gfxPrefs::ImageMemAnimatedDiscardable()) {
419 // We never discard frames of animated images.
422 if (mAnimationState
->GetCompositedFrameInvalid()) {
423 // We're not going to draw anything at all.
429 // If we are not locked our decoded data could get discard at any time (ie
430 // between the call to this function and when we are asked to draw), so we
431 // have to return false if we are unlocked.
432 if (mLockCount
== 0) {
436 LookupResult result
= SurfaceCache::LookupBestMatch(
438 RasterSurfaceKey(mSize
, DefaultSurfaceFlags(), PlaybackType::eStatic
),
439 /* aMarkUsed = */ false);
440 MatchType matchType
= result
.Type();
441 if (matchType
== MatchType::NOT_FOUND
|| matchType
== MatchType::PENDING
||
442 !result
.Surface()->IsFinished()) {
449 void RasterImage::OnSurfaceDiscarded(const SurfaceKey
& aSurfaceKey
) {
450 MOZ_ASSERT(mProgressTracker
);
452 bool animatedFramesDiscarded
=
453 mAnimationState
&& aSurfaceKey
.Playback() == PlaybackType::eAnimated
;
455 nsCOMPtr
<nsIEventTarget
> eventTarget
;
456 if (mProgressTracker
) {
457 eventTarget
= mProgressTracker
->GetEventTarget();
459 eventTarget
= do_GetMainThread();
462 RefPtr
<RasterImage
> image
= this;
463 nsCOMPtr
<nsIRunnable
> ev
=
464 NS_NewRunnableFunction("RasterImage::OnSurfaceDiscarded", [=]() -> void {
465 image
->OnSurfaceDiscardedInternal(animatedFramesDiscarded
);
467 eventTarget
->Dispatch(ev
.forget(), NS_DISPATCH_NORMAL
);
470 void RasterImage::OnSurfaceDiscardedInternal(bool aAnimatedFramesDiscarded
) {
471 MOZ_ASSERT(NS_IsMainThread());
473 if (aAnimatedFramesDiscarded
&& mAnimationState
) {
474 MOZ_ASSERT(gfxPrefs::ImageMemAnimatedDiscardable());
475 ReleaseImageContainer();
477 mAnimationState
->UpdateState(mAnimationFinished
, this, mSize
);
478 NotifyProgress(NoProgress
, rect
);
481 if (mProgressTracker
) {
482 mProgressTracker
->OnDiscard();
486 //******************************************************************************
488 RasterImage::GetAnimated(bool* aAnimated
) {
490 return NS_ERROR_FAILURE
;
493 NS_ENSURE_ARG_POINTER(aAnimated
);
495 // If we have an AnimationState, we can know for sure.
496 if (mAnimationState
) {
501 // Otherwise, we need to have been decoded to know for sure, since if we were
502 // decoded at least once mAnimationState would have been created for animated
503 // images. This is true even though we check for animation during the
504 // metadata decode, because we may still discover animation only during the
505 // full decode for corrupt images.
506 if (!mHasBeenDecoded
) {
507 return NS_ERROR_NOT_AVAILABLE
;
516 //******************************************************************************
517 NS_IMETHODIMP_(int32_t)
518 RasterImage::GetFirstFrameDelay() {
523 bool animated
= false;
524 if (NS_FAILED(GetAnimated(&animated
)) || !animated
) {
528 MOZ_ASSERT(mAnimationState
, "Animated images should have an AnimationState");
529 return mAnimationState
->FirstFrameTimeout().AsEncodedValueDeprecated();
532 NS_IMETHODIMP_(already_AddRefed
<SourceSurface
>)
533 RasterImage::GetFrame(uint32_t aWhichFrame
, uint32_t aFlags
) {
534 return GetFrameAtSize(mSize
, aWhichFrame
, aFlags
);
537 NS_IMETHODIMP_(already_AddRefed
<SourceSurface
>)
538 RasterImage::GetFrameAtSize(const IntSize
& aSize
, uint32_t aWhichFrame
,
541 NotifyDrawingObservers();
544 auto result
= GetFrameInternal(aSize
, Nothing(), aWhichFrame
, aFlags
);
545 return mozilla::Get
<2>(result
).forget();
548 Tuple
<ImgDrawResult
, IntSize
, RefPtr
<SourceSurface
>>
549 RasterImage::GetFrameInternal(const IntSize
& aSize
,
550 const Maybe
<SVGImageContext
>& aSVGContext
,
551 uint32_t aWhichFrame
, uint32_t aFlags
) {
552 MOZ_ASSERT(aWhichFrame
<= FRAME_MAX_VALUE
);
554 if (aSize
.IsEmpty() || aWhichFrame
> FRAME_MAX_VALUE
) {
555 return MakeTuple(ImgDrawResult::BAD_ARGS
, aSize
, RefPtr
<SourceSurface
>());
559 return MakeTuple(ImgDrawResult::BAD_IMAGE
, aSize
, RefPtr
<SourceSurface
>());
562 // Get the frame. If it's not there, it's probably the caller's fault for
563 // not waiting for the data to be loaded from the network or not passing
565 LookupResult result
= LookupFrame(aSize
, aFlags
, ToPlaybackType(aWhichFrame
),
566 /* aMarkUsed = */ true);
568 // The surface cache may have suggested we use a different size than the
569 // given size in the future. This may or may not be accompanied by an
570 // actual surface, depending on what it has in its cache.
571 IntSize suggestedSize
=
572 result
.SuggestedSize().IsEmpty() ? aSize
: result
.SuggestedSize();
573 MOZ_ASSERT_IF(result
.Type() == MatchType::SUBSTITUTE_BECAUSE_BEST
,
574 suggestedSize
!= aSize
);
577 // The OS threw this frame away and we couldn't redecode it.
578 return MakeTuple(ImgDrawResult::TEMPORARY_ERROR
, suggestedSize
,
579 RefPtr
<SourceSurface
>());
582 RefPtr
<SourceSurface
> surface
= result
.Surface()->GetSourceSurface();
583 if (!result
.Surface()->IsFinished()) {
584 return MakeTuple(ImgDrawResult::INCOMPLETE
, suggestedSize
,
588 return MakeTuple(ImgDrawResult::SUCCESS
, suggestedSize
, std::move(surface
));
591 Tuple
<ImgDrawResult
, IntSize
> RasterImage::GetImageContainerSize(
592 LayerManager
* aManager
, const IntSize
& aSize
, uint32_t aFlags
) {
594 return MakeTuple(ImgDrawResult::NOT_READY
, IntSize(0, 0));
597 if (aSize
.IsEmpty()) {
598 return MakeTuple(ImgDrawResult::BAD_ARGS
, IntSize(0, 0));
601 // We check the minimum size because while we support downscaling, we do not
602 // support upscaling. If aSize > mSize, we will never give a larger surface
603 // than mSize. If mSize > aSize, and mSize > maxTextureSize, we still want to
604 // use image containers if aSize <= maxTextureSize.
605 int32_t maxTextureSize
= aManager
->GetMaxTextureSize();
606 if (min(mSize
.width
, aSize
.width
) > maxTextureSize
||
607 min(mSize
.height
, aSize
.height
) > maxTextureSize
) {
608 return MakeTuple(ImgDrawResult::NOT_SUPPORTED
, IntSize(0, 0));
611 if (!CanDownscaleDuringDecode(aSize
, aFlags
)) {
612 return MakeTuple(ImgDrawResult::SUCCESS
, mSize
);
615 return MakeTuple(ImgDrawResult::SUCCESS
, aSize
);
619 RasterImage::IsImageContainerAvailable(LayerManager
* aManager
,
621 return IsImageContainerAvailableAtSize(aManager
, mSize
, aFlags
);
624 NS_IMETHODIMP_(already_AddRefed
<ImageContainer
>)
625 RasterImage::GetImageContainer(LayerManager
* aManager
, uint32_t aFlags
) {
626 RefPtr
<ImageContainer
> container
;
627 ImgDrawResult drawResult
= GetImageContainerImpl(
628 aManager
, mSize
, Nothing(), aFlags
, getter_AddRefs(container
));
630 // We silence the unused warning here because anything that needs the draw
631 // result should be using GetImageContainerAtSize, not GetImageContainer.
633 return container
.forget();
637 RasterImage::IsImageContainerAvailableAtSize(LayerManager
* aManager
,
638 const IntSize
& aSize
,
640 // We check the minimum size because while we support downscaling, we do not
641 // support upscaling. If aSize > mSize, we will never give a larger surface
642 // than mSize. If mSize > aSize, and mSize > maxTextureSize, we still want to
643 // use image containers if aSize <= maxTextureSize.
644 int32_t maxTextureSize
= aManager
->GetMaxTextureSize();
645 if (!mHasSize
|| aSize
.IsEmpty() ||
646 min(mSize
.width
, aSize
.width
) > maxTextureSize
||
647 min(mSize
.height
, aSize
.height
) > maxTextureSize
) {
654 NS_IMETHODIMP_(ImgDrawResult
)
655 RasterImage::GetImageContainerAtSize(layers::LayerManager
* aManager
,
656 const gfx::IntSize
& aSize
,
657 const Maybe
<SVGImageContext
>& aSVGContext
,
659 layers::ImageContainer
** aOutContainer
) {
660 // We do not pass in the given SVG context because in theory it could differ
661 // between calls, but actually have no impact on the actual contents of the
663 return GetImageContainerImpl(aManager
, aSize
, Nothing(), aFlags
,
667 size_t RasterImage::SizeOfSourceWithComputedFallback(
668 SizeOfState
& aState
) const {
669 return mSourceBuffer
->SizeOfIncludingThisWithComputedFallback(
670 aState
.mMallocSizeOf
);
673 void RasterImage::CollectSizeOfSurfaces(
674 nsTArray
<SurfaceMemoryCounter
>& aCounters
,
675 MallocSizeOf aMallocSizeOf
) const {
676 SurfaceCache::CollectSizeOfSurfaces(ImageKey(this), aCounters
, aMallocSizeOf
);
677 if (mFrameAnimator
) {
678 mFrameAnimator
->CollectSizeOfCompositingSurfaces(aCounters
, aMallocSizeOf
);
682 bool RasterImage::SetMetadata(const ImageMetadata
& aMetadata
,
683 bool aFromMetadataDecode
) {
684 MOZ_ASSERT(NS_IsMainThread());
690 if (aMetadata
.HasSize()) {
691 IntSize size
= aMetadata
.GetSize();
692 if (size
.width
< 0 || size
.height
< 0) {
693 NS_WARNING("Image has negative intrinsic size");
698 MOZ_ASSERT(aMetadata
.HasOrientation());
699 Orientation orientation
= aMetadata
.GetOrientation();
701 // If we already have a size, check the new size against the old one.
702 if (mHasSize
&& (size
!= mSize
|| orientation
!= mOrientation
)) {
704 "Image changed size or orientation on redecode! "
705 "This should not happen!");
710 // Set the size and flag that we have it.
712 mOrientation
= orientation
;
713 mNativeSizes
= aMetadata
.GetNativeSizes();
717 if (mHasSize
&& aMetadata
.HasAnimation() && !mAnimationState
) {
718 // We're becoming animated, so initialize animation stuff.
719 mAnimationState
.emplace(mAnimationMode
);
720 mFrameAnimator
= MakeUnique
<FrameAnimator
>(this, mSize
);
722 if (!gfxPrefs::ImageMemAnimatedDiscardable()) {
723 // We don't support discarding animated images (See bug 414259).
724 // Lock the image and throw away the key.
728 if (!aFromMetadataDecode
) {
729 // The metadata decode reported that this image isn't animated, but we
730 // discovered that it actually was during the full decode. This is a
731 // rare failure that only occurs for corrupt images. To recover, we need
732 // to discard all existing surfaces and redecode.
737 if (mAnimationState
) {
738 mAnimationState
->SetLoopCount(aMetadata
.GetLoopCount());
739 mAnimationState
->SetFirstFrameTimeout(aMetadata
.GetFirstFrameTimeout());
741 if (aMetadata
.HasLoopLength()) {
742 mAnimationState
->SetLoopLength(aMetadata
.GetLoopLength());
744 if (aMetadata
.HasFirstFrameRefreshArea()) {
745 mAnimationState
->SetFirstFrameRefreshArea(
746 aMetadata
.GetFirstFrameRefreshArea());
750 if (aMetadata
.HasHotspot()) {
751 IntPoint hotspot
= aMetadata
.GetHotspot();
753 nsCOMPtr
<nsISupportsPRUint32
> intwrapx
=
754 do_CreateInstance(NS_SUPPORTS_PRUINT32_CONTRACTID
);
755 nsCOMPtr
<nsISupportsPRUint32
> intwrapy
=
756 do_CreateInstance(NS_SUPPORTS_PRUINT32_CONTRACTID
);
757 intwrapx
->SetData(hotspot
.x
);
758 intwrapy
->SetData(hotspot
.y
);
760 Set("hotspotX", intwrapx
);
761 Set("hotspotY", intwrapy
);
768 RasterImage::SetAnimationMode(uint16_t aAnimationMode
) {
769 if (mAnimationState
) {
770 mAnimationState
->SetAnimationMode(aAnimationMode
);
772 return SetAnimationModeInternal(aAnimationMode
);
775 //******************************************************************************
777 nsresult
RasterImage::StartAnimation() {
779 return NS_ERROR_FAILURE
;
782 MOZ_ASSERT(ShouldAnimate(), "Should not animate!");
784 // If we're not ready to animate, then set mPendingAnimation, which will cause
785 // us to start animating if and when we do become ready.
787 !mAnimationState
|| mAnimationState
->KnownFrameCount() < 1;
788 if (mPendingAnimation
) {
792 // Don't bother to animate if we're displaying the first frame forever.
793 if (mAnimationState
->GetCurrentAnimationFrameIndex() == 0 &&
794 mAnimationState
->FirstFrameTimeout() == FrameTimeout::Forever()) {
795 mAnimationFinished
= true;
796 return NS_ERROR_ABORT
;
799 // We need to set the time that this initial frame was first displayed, as
800 // this is used in AdvanceFrame().
801 mAnimationState
->InitAnimationFrameTimeIfNecessary();
806 //******************************************************************************
807 nsresult
RasterImage::StopAnimation() {
808 MOZ_ASSERT(mAnimating
, "Should be animating!");
812 rv
= NS_ERROR_FAILURE
;
814 mAnimationState
->SetAnimationFrameTime(TimeStamp());
821 //******************************************************************************
823 RasterImage::ResetAnimation() {
825 return NS_ERROR_FAILURE
;
828 mPendingAnimation
= false;
830 if (mAnimationMode
== kDontAnimMode
|| !mAnimationState
||
831 mAnimationState
->GetCurrentAnimationFrameIndex() == 0) {
835 mAnimationFinished
= false;
841 MOZ_ASSERT(mAnimationState
, "Should have AnimationState");
842 MOZ_ASSERT(mFrameAnimator
, "Should have FrameAnimator");
843 mFrameAnimator
->ResetAnimation(*mAnimationState
);
845 NotifyProgress(NoProgress
, mAnimationState
->FirstFrameRefreshArea());
847 // Start the animation again. It may not have been running before, if
848 // mAnimationFinished was true before entering this function.
854 //******************************************************************************
856 RasterImage::SetAnimationStartTime(const TimeStamp
& aTime
) {
857 if (mError
|| mAnimationMode
== kDontAnimMode
|| mAnimating
||
862 mAnimationState
->SetAnimationFrameTime(aTime
);
865 NS_IMETHODIMP_(float)
866 RasterImage::GetFrameIndex(uint32_t aWhichFrame
) {
867 MOZ_ASSERT(aWhichFrame
<= FRAME_MAX_VALUE
, "Invalid argument");
868 return (aWhichFrame
== FRAME_FIRST
|| !mAnimationState
)
870 : mAnimationState
->GetCurrentAnimationFrameIndex();
873 NS_IMETHODIMP_(IntRect
)
874 RasterImage::GetImageSpaceInvalidationRect(const IntRect
& aRect
) {
878 nsresult
RasterImage::OnImageDataComplete(nsIRequest
*, nsISupports
*,
879 nsresult aStatus
, bool aLastPart
) {
880 MOZ_ASSERT(NS_IsMainThread());
882 // Record that we have all the data we're going to get now.
883 mAllSourceData
= true;
885 // Let decoders know that there won't be any more data coming.
886 mSourceBuffer
->Complete(aStatus
);
888 // Allow a synchronous metadata decode if mSyncLoad was set, or if we're
889 // running on a single thread (in which case waiting for the async metadata
890 // decoder could delay this image's load event quite a bit), or if this image
892 bool canSyncDecodeMetadata
=
893 mSyncLoad
|| mTransient
|| DecodePool::NumberOfCores() < 2;
895 if (canSyncDecodeMetadata
&& !mHasSize
) {
896 // We're loading this image synchronously, so it needs to be usable after
897 // this call returns. Since we haven't gotten our size yet, we need to do a
898 // synchronous metadata decode here.
899 DecodeMetadata(FLAG_SYNC_DECODE
);
902 // Determine our final status, giving precedence to Necko failure codes. We
903 // check after running the metadata decode in case it triggered an error.
904 nsresult finalStatus
= mError
? NS_ERROR_FAILURE
: NS_OK
;
905 if (NS_FAILED(aStatus
)) {
906 finalStatus
= aStatus
;
909 // If loading failed, report an error.
910 if (NS_FAILED(finalStatus
)) {
914 Progress loadProgress
= LoadCompleteProgress(aLastPart
, mError
, finalStatus
);
916 if (!mHasSize
&& !mError
) {
917 // We don't have our size yet, so we'll fire the load event in SetSize().
918 MOZ_ASSERT(!canSyncDecodeMetadata
,
919 "Firing load async after metadata sync decode?");
920 mLoadProgress
= Some(loadProgress
);
924 NotifyForLoadEvent(loadProgress
);
929 void RasterImage::NotifyForLoadEvent(Progress aProgress
) {
930 MOZ_ASSERT(mHasSize
|| mError
, "Need to know size before firing load event");
932 !mHasSize
|| (mProgressTracker
->GetProgress() & FLAG_SIZE_AVAILABLE
),
933 "Should have notified that the size is available if we have it");
935 // If we encountered an error, make sure we notify for that as well.
937 aProgress
|= FLAG_HAS_ERROR
;
940 // Notify our listeners, which will fire this image's load event.
941 NotifyProgress(aProgress
);
944 nsresult
RasterImage::OnImageDataAvailable(nsIRequest
*, nsISupports
*,
945 nsIInputStream
* aInputStream
,
946 uint64_t, uint32_t aCount
) {
947 nsresult rv
= mSourceBuffer
->AppendFromInputStream(aInputStream
, aCount
);
948 if (NS_SUCCEEDED(rv
) && !mSomeSourceData
) {
949 mSomeSourceData
= true;
951 // Create an async metadata decoder and verify we succeed in doing so.
952 rv
= DecodeMetadata(DECODE_FLAGS_DEFAULT
);
962 nsresult
RasterImage::SetSourceSizeHint(uint32_t aSizeHint
) {
963 if (aSizeHint
== 0) {
967 nsresult rv
= mSourceBuffer
->ExpectLength(aSizeHint
);
968 if (rv
== NS_ERROR_OUT_OF_MEMORY
) {
969 // Flush memory, try to get some back, and try again.
970 rv
= nsMemory::HeapMinimize(true);
971 if (NS_SUCCEEDED(rv
)) {
972 rv
= mSourceBuffer
->ExpectLength(aSizeHint
);
979 /********* Methods to implement lazy allocation of nsIProperties object *******/
981 RasterImage::Get(const char* prop
, const nsIID
& iid
, void** result
) {
983 return NS_ERROR_FAILURE
;
985 return mProperties
->Get(prop
, iid
, result
);
989 RasterImage::Set(const char* prop
, nsISupports
* value
) {
991 mProperties
= new nsProperties();
993 return mProperties
->Set(prop
, value
);
997 RasterImage::Has(const char* prop
, bool* _retval
) {
998 NS_ENSURE_ARG_POINTER(_retval
);
1003 return mProperties
->Has(prop
, _retval
);
1007 RasterImage::Undefine(const char* prop
) {
1009 return NS_ERROR_FAILURE
;
1011 return mProperties
->Undefine(prop
);
1015 RasterImage::GetKeys(uint32_t* count
, char*** keys
) {
1021 return mProperties
->GetKeys(count
, keys
);
1024 void RasterImage::Discard() {
1025 MOZ_ASSERT(NS_IsMainThread());
1026 MOZ_ASSERT(CanDiscard(), "Asked to discard but can't");
1027 MOZ_ASSERT(!mAnimationState
|| gfxPrefs::ImageMemAnimatedDiscardable(),
1028 "Asked to discard for animated image");
1030 // Delete all the decoded frames.
1031 SurfaceCache::RemoveImage(ImageKey(this));
1033 if (mAnimationState
) {
1034 ReleaseImageContainer();
1036 mAnimationState
->UpdateState(mAnimationFinished
, this, mSize
);
1037 NotifyProgress(NoProgress
, rect
);
1040 // Notify that we discarded.
1041 if (mProgressTracker
) {
1042 mProgressTracker
->OnDiscard();
1046 bool RasterImage::CanDiscard() {
1047 return mAllSourceData
&&
1048 // Can discard animated images if the pref is set
1049 (!mAnimationState
|| gfxPrefs::ImageMemAnimatedDiscardable());
1053 RasterImage::StartDecoding(uint32_t aFlags
) {
1055 return NS_ERROR_FAILURE
;
1059 mWantFullDecode
= true;
1063 uint32_t flags
= (aFlags
& FLAG_ASYNC_NOTIFY
) | FLAG_SYNC_DECODE_IF_FAST
|
1064 FLAG_HIGH_QUALITY_SCALING
;
1065 return RequestDecodeForSize(mSize
, flags
);
1068 bool RasterImage::StartDecodingWithResult(uint32_t aFlags
) {
1074 mWantFullDecode
= true;
1078 uint32_t flags
= (aFlags
& FLAG_ASYNC_NOTIFY
) | FLAG_SYNC_DECODE_IF_FAST
|
1079 FLAG_HIGH_QUALITY_SCALING
;
1080 DrawableSurface surface
= RequestDecodeForSizeInternal(mSize
, flags
);
1081 return surface
&& surface
->IsFinished();
1085 RasterImage::RequestDecodeForSize(const IntSize
& aSize
, uint32_t aFlags
) {
1086 MOZ_ASSERT(NS_IsMainThread());
1089 return NS_ERROR_FAILURE
;
1092 RequestDecodeForSizeInternal(aSize
, aFlags
);
1097 DrawableSurface
RasterImage::RequestDecodeForSizeInternal(const IntSize
& aSize
,
1099 MOZ_ASSERT(NS_IsMainThread());
1102 return DrawableSurface();
1106 mWantFullDecode
= true;
1107 return DrawableSurface();
1110 // Decide whether to sync decode images we can decode quickly. Here we are
1111 // explicitly trading off flashing for responsiveness in the case that we're
1112 // redecoding an image (see bug 845147).
1113 bool shouldSyncDecodeIfFast
=
1114 !mHasBeenDecoded
&& (aFlags
& FLAG_SYNC_DECODE_IF_FAST
);
1117 shouldSyncDecodeIfFast
? aFlags
: aFlags
& ~FLAG_SYNC_DECODE_IF_FAST
;
1119 // Perform a frame lookup, which will implicitly start decoding if needed.
1120 PlaybackType playbackType
=
1121 mAnimationState
? PlaybackType::eAnimated
: PlaybackType::eStatic
;
1122 LookupResult result
=
1123 LookupFrame(aSize
, flags
, playbackType
, /* aMarkUsed = */ false);
1124 return std::move(result
.Surface());
1127 static bool LaunchDecodingTask(IDecodingTask
* aTask
, RasterImage
* aImage
,
1128 uint32_t aFlags
, bool aHaveSourceData
) {
1129 if (aHaveSourceData
) {
1130 nsCString
uri(aImage
->GetURIString());
1132 // If we have all the data, we can sync decode if requested.
1133 if (aFlags
& imgIContainer::FLAG_SYNC_DECODE
) {
1134 DecodePool::Singleton()->SyncRunIfPossible(aTask
, uri
);
1138 if (aFlags
& imgIContainer::FLAG_SYNC_DECODE_IF_FAST
) {
1139 return DecodePool::Singleton()->SyncRunIfPreferred(aTask
, uri
);
1143 // Perform an async decode. We also take this path if we don't have all the
1144 // source data yet, since sync decoding is impossible in that situation.
1145 DecodePool::Singleton()->AsyncRun(aTask
);
1149 bool RasterImage::Decode(const IntSize
& aSize
, uint32_t aFlags
,
1150 PlaybackType aPlaybackType
) {
1151 MOZ_ASSERT(NS_IsMainThread());
1157 // If we don't have a size yet, we can't do any other decoding.
1159 mWantFullDecode
= true;
1163 // We're about to decode again, which may mean that some of the previous sizes
1164 // we've decoded at aren't useful anymore. We can allow them to expire from
1165 // the cache by unlocking them here. When the decode finishes, it will send an
1166 // invalidation that will cause all instances of this image to redraw. If this
1167 // image is locked, any surfaces that are still useful will become locked
1168 // again when LookupFrame touches them, and the remainder will eventually
1170 SurfaceCache::UnlockEntries(ImageKey(this));
1172 // Determine which flags we need to decode this image with.
1173 DecoderFlags decoderFlags
= DefaultDecoderFlags();
1174 if (aFlags
& FLAG_ASYNC_NOTIFY
) {
1175 decoderFlags
|= DecoderFlags::ASYNC_NOTIFY
;
1178 decoderFlags
|= DecoderFlags::IMAGE_IS_TRANSIENT
;
1180 if (mHasBeenDecoded
) {
1181 decoderFlags
|= DecoderFlags::IS_REDECODE
;
1183 if ((aFlags
& FLAG_SYNC_DECODE
) || !(aFlags
& FLAG_HIGH_QUALITY_SCALING
)) {
1184 // Used SurfaceCache::Lookup instead of SurfaceCache::LookupBestMatch. That
1185 // means the caller can handle a differently sized surface to be returned
1187 decoderFlags
|= DecoderFlags::CANNOT_SUBSTITUTE
;
1190 SurfaceFlags surfaceFlags
= ToSurfaceFlags(aFlags
);
1192 // If there's no transparency, it doesn't matter whether we premultiply
1194 surfaceFlags
&= ~SurfaceFlags::NO_PREMULTIPLY_ALPHA
;
1197 // Create a decoder.
1198 RefPtr
<IDecodingTask
> task
;
1200 bool animated
= mAnimationState
&& aPlaybackType
== PlaybackType::eAnimated
;
1202 if (gfxPrefs::ImageAnimatedGenerateFullFrames()) {
1203 decoderFlags
|= DecoderFlags::BLEND_ANIMATION
;
1206 size_t currentFrame
= mAnimationState
->GetCurrentAnimationFrameIndex();
1207 rv
= DecoderFactory::CreateAnimationDecoder(
1208 mDecoderType
, WrapNotNull(this), mSourceBuffer
, mSize
, decoderFlags
,
1209 surfaceFlags
, currentFrame
, getter_AddRefs(task
));
1211 rv
= DecoderFactory::CreateDecoder(
1212 mDecoderType
, WrapNotNull(this), mSourceBuffer
, mSize
, aSize
,
1213 decoderFlags
, surfaceFlags
, getter_AddRefs(task
));
1216 if (rv
== NS_ERROR_ALREADY_INITIALIZED
) {
1217 // We raced with an already pending decoder, and it finished before we
1218 // managed to insert the new decoder. Pretend we did a sync call to make
1219 // the caller lookup in the surface cache again.
1225 // We pass false for aAllowInvalidation because we may be asked to use
1226 // async notifications. Any potential invalidation here will be sent when
1227 // RequestRefresh is called, or NotifyDecodeComplete.
1231 mAnimationState
->UpdateState(mAnimationFinished
, this, mSize
, false);
1232 MOZ_ASSERT(rect
.IsEmpty());
1235 // Make sure DecoderFactory was able to create a decoder successfully.
1236 if (NS_FAILED(rv
)) {
1244 // We're ready to decode; start the decoder.
1245 return LaunchDecodingTask(task
, this, aFlags
, mAllSourceData
);
1249 RasterImage::DecodeMetadata(uint32_t aFlags
) {
1251 return NS_ERROR_FAILURE
;
1254 MOZ_ASSERT(!mHasSize
, "Should not do unnecessary metadata decodes");
1256 // Create a decoder.
1257 RefPtr
<IDecodingTask
> task
= DecoderFactory::CreateMetadataDecoder(
1258 mDecoderType
, WrapNotNull(this), mSourceBuffer
);
1260 // Make sure DecoderFactory was able to create a decoder successfully.
1262 return NS_ERROR_FAILURE
;
1265 // We're ready to decode; start the decoder.
1266 LaunchDecodingTask(task
, this, aFlags
, mAllSourceData
);
1270 void RasterImage::RecoverFromInvalidFrames(const IntSize
& aSize
,
1276 NS_WARNING("A RasterImage's frames became invalid. Attempting to recover...");
1278 // Discard all existing frames, since they're probably all now invalid.
1279 SurfaceCache::RemoveImage(ImageKey(this));
1281 // Relock the image if it's supposed to be locked.
1282 if (mLockCount
> 0) {
1283 SurfaceCache::LockImage(ImageKey(this));
1286 // Animated images require some special handling, because we normally require
1287 // that they never be discarded.
1288 if (mAnimationState
) {
1289 Decode(mSize
, aFlags
| FLAG_SYNC_DECODE
, PlaybackType::eAnimated
);
1294 // For non-animated images, it's fine to recover using an async decode.
1295 Decode(aSize
, aFlags
, PlaybackType::eStatic
);
1298 static bool HaveSkia() {
1299 #ifdef MOZ_ENABLE_SKIA
1306 bool RasterImage::CanDownscaleDuringDecode(const IntSize
& aSize
,
1308 // Check basic requirements: downscale-during-decode is enabled, Skia is
1309 // available, this image isn't transient, we have all the source data and know
1310 // our size, and the flags allow us to do it.
1311 if (!mHasSize
|| mTransient
|| !HaveSkia() ||
1312 !gfxPrefs::ImageDownscaleDuringDecodeEnabled() ||
1313 !(aFlags
& imgIContainer::FLAG_HIGH_QUALITY_SCALING
)) {
1317 // We don't downscale animated images during decode.
1318 if (mAnimationState
) {
1323 if (aSize
.width
>= mSize
.width
|| aSize
.height
>= mSize
.height
) {
1327 // Zero or negative width or height is unacceptable.
1328 if (aSize
.width
< 1 || aSize
.height
< 1) {
1332 // There's no point in scaling if we can't store the result.
1333 if (!SurfaceCache::CanHold(aSize
)) {
1340 ImgDrawResult
RasterImage::DrawInternal(DrawableSurface
&& aSurface
,
1341 gfxContext
* aContext
,
1342 const IntSize
& aSize
,
1343 const ImageRegion
& aRegion
,
1344 SamplingFilter aSamplingFilter
,
1345 uint32_t aFlags
, float aOpacity
) {
1346 gfxContextMatrixAutoSaveRestore
saveMatrix(aContext
);
1347 ImageRegion
region(aRegion
);
1348 bool frameIsFinished
= aSurface
->IsFinished();
1351 NotifyDrawingObservers();
1354 // By now we may have a frame with the requested size. If not, we need to
1355 // adjust the drawing parameters accordingly.
1356 IntSize finalSize
= aSurface
->GetImageSize();
1357 bool couldRedecodeForBetterFrame
= false;
1358 if (finalSize
!= aSize
) {
1359 gfx::Size
scale(double(aSize
.width
) / finalSize
.width
,
1360 double(aSize
.height
) / finalSize
.height
);
1361 aContext
->Multiply(gfxMatrix::Scaling(scale
.width
, scale
.height
));
1362 region
.Scale(1.0 / scale
.width
, 1.0 / scale
.height
);
1364 couldRedecodeForBetterFrame
= CanDownscaleDuringDecode(aSize
, aFlags
);
1367 if (!aSurface
->Draw(aContext
, region
, aSamplingFilter
, aFlags
, aOpacity
)) {
1368 RecoverFromInvalidFrames(aSize
, aFlags
);
1369 return ImgDrawResult::TEMPORARY_ERROR
;
1371 if (!frameIsFinished
) {
1372 return ImgDrawResult::INCOMPLETE
;
1374 if (couldRedecodeForBetterFrame
) {
1375 return ImgDrawResult::WRONG_SIZE
;
1377 return ImgDrawResult::SUCCESS
;
1380 //******************************************************************************
1381 NS_IMETHODIMP_(ImgDrawResult
)
1382 RasterImage::Draw(gfxContext
* aContext
, const IntSize
& aSize
,
1383 const ImageRegion
& aRegion
, uint32_t aWhichFrame
,
1384 SamplingFilter aSamplingFilter
,
1385 const Maybe
<SVGImageContext
>& /*aSVGContext - ignored*/,
1386 uint32_t aFlags
, float aOpacity
) {
1387 if (aWhichFrame
> FRAME_MAX_VALUE
) {
1388 return ImgDrawResult::BAD_ARGS
;
1392 return ImgDrawResult::BAD_IMAGE
;
1395 // Illegal -- you can't draw with non-default decode flags.
1396 // (Disabling colorspace conversion might make sense to allow, but
1397 // we don't currently.)
1398 if (ToSurfaceFlags(aFlags
) != DefaultSurfaceFlags()) {
1399 return ImgDrawResult::BAD_ARGS
;
1403 return ImgDrawResult::BAD_ARGS
;
1406 if (mAnimationConsumers
== 0) {
1407 SendOnUnlockedDraw(aFlags
);
1410 // If we're not using SamplingFilter::GOOD, we shouldn't high-quality scale or
1411 // downscale during decode.
1412 uint32_t flags
= aSamplingFilter
== SamplingFilter::GOOD
1414 : aFlags
& ~FLAG_HIGH_QUALITY_SCALING
;
1416 LookupResult result
= LookupFrame(aSize
, flags
, ToPlaybackType(aWhichFrame
),
1417 /* aMarkUsed = */ true);
1419 // Getting the frame (above) touches the image and kicks off decoding.
1420 if (mDrawStartTime
.IsNull()) {
1421 mDrawStartTime
= TimeStamp::Now();
1423 return ImgDrawResult::NOT_READY
;
1426 bool shouldRecordTelemetry
=
1427 !mDrawStartTime
.IsNull() && result
.Surface()->IsFinished();
1429 auto drawResult
= DrawInternal(std::move(result
.Surface()), aContext
, aSize
,
1430 aRegion
, aSamplingFilter
, flags
, aOpacity
);
1432 if (shouldRecordTelemetry
) {
1433 TimeDuration drawLatency
= TimeStamp::Now() - mDrawStartTime
;
1434 Telemetry::Accumulate(Telemetry::IMAGE_DECODE_ON_DRAW_LATENCY
,
1435 int32_t(drawLatency
.ToMicroseconds()));
1436 mDrawStartTime
= TimeStamp();
1442 //******************************************************************************
1445 RasterImage::LockImage() {
1446 MOZ_ASSERT(NS_IsMainThread(),
1447 "Main thread to encourage serialization with UnlockImage");
1449 return NS_ERROR_FAILURE
;
1452 // Increment the lock count
1455 // Lock this image's surfaces in the SurfaceCache.
1456 if (mLockCount
== 1) {
1457 SurfaceCache::LockImage(ImageKey(this));
1463 //******************************************************************************
1466 RasterImage::UnlockImage() {
1467 MOZ_ASSERT(NS_IsMainThread(),
1468 "Main thread to encourage serialization with LockImage");
1470 return NS_ERROR_FAILURE
;
1473 // It's an error to call this function if the lock count is 0
1474 MOZ_ASSERT(mLockCount
> 0, "Calling UnlockImage with mLockCount == 0!");
1475 if (mLockCount
== 0) {
1476 return NS_ERROR_ABORT
;
1479 // Decrement our lock count
1482 // Unlock this image's surfaces in the SurfaceCache.
1483 if (mLockCount
== 0) {
1484 SurfaceCache::UnlockImage(ImageKey(this));
1490 //******************************************************************************
1493 RasterImage::RequestDiscard() {
1494 if (mDiscardable
&& // Enabled at creation time...
1495 mLockCount
== 0 && // ...not temporarily disabled...
1503 // Indempotent error flagging routine. If a decoder is open, shuts it down.
1504 void RasterImage::DoError() {
1505 // If we've flagged an error before, we have nothing to do
1510 // We can't safely handle errors off-main-thread, so dispatch a worker to
1512 if (!NS_IsMainThread()) {
1513 HandleErrorWorker::DispatchIfNeeded(this);
1517 // Put the container in an error state.
1520 // Stop animation and release our FrameAnimator.
1524 mAnimationState
= Nothing();
1525 mFrameAnimator
= nullptr;
1527 // Release all locks.
1529 SurfaceCache::UnlockImage(ImageKey(this));
1531 // Release all frames from the surface cache.
1532 SurfaceCache::RemoveImage(ImageKey(this));
1534 // Invalidate to get rid of any partially-drawn image content.
1535 NotifyProgress(NoProgress
, IntRect(0, 0, mSize
.width
, mSize
.height
));
1537 MOZ_LOG(gImgLog
, LogLevel::Error
,
1538 ("RasterImage: [this=%p] Error detected for image\n", this));
1541 /* static */ void RasterImage::HandleErrorWorker::DispatchIfNeeded(
1542 RasterImage
* aImage
) {
1543 RefPtr
<HandleErrorWorker
> worker
= new HandleErrorWorker(aImage
);
1544 NS_DispatchToMainThread(worker
);
1547 RasterImage::HandleErrorWorker::HandleErrorWorker(RasterImage
* aImage
)
1548 : Runnable("image::RasterImage::HandleErrorWorker"), mImage(aImage
) {
1549 MOZ_ASSERT(mImage
, "Should have image");
1553 RasterImage::HandleErrorWorker::Run() {
1559 bool RasterImage::ShouldAnimate() {
1560 return ImageResource::ShouldAnimate() && mAnimationState
&&
1561 mAnimationState
->KnownFrameCount() >= 1 && !mAnimationFinished
;
1566 RasterImage::GetFramesNotified(uint32_t* aFramesNotified
) {
1567 NS_ENSURE_ARG_POINTER(aFramesNotified
);
1569 *aFramesNotified
= mFramesNotified
;
1575 void RasterImage::NotifyProgress(
1576 Progress aProgress
, const IntRect
& aInvalidRect
/* = IntRect() */,
1577 const Maybe
<uint32_t>& aFrameCount
/* = Nothing() */,
1578 DecoderFlags aDecoderFlags
1579 /* = DefaultDecoderFlags() */,
1580 SurfaceFlags aSurfaceFlags
1581 /* = DefaultSurfaceFlags() */) {
1582 MOZ_ASSERT(NS_IsMainThread());
1584 // Ensure that we stay alive long enough to finish notifying.
1585 RefPtr
<RasterImage
> image
= this;
1587 const bool wasDefaultFlags
= aSurfaceFlags
== DefaultSurfaceFlags();
1589 if (!aInvalidRect
.IsEmpty() && wasDefaultFlags
) {
1590 // Update our image container since we're invalidating.
1591 UpdateImageContainer(Some(aInvalidRect
));
1594 if (!(aDecoderFlags
& DecoderFlags::FIRST_FRAME_ONLY
)) {
1595 // We may have decoded new animation frames; update our animation state.
1596 MOZ_ASSERT_IF(aFrameCount
&& *aFrameCount
> 1, mAnimationState
|| mError
);
1597 if (mAnimationState
&& aFrameCount
) {
1598 mAnimationState
->UpdateKnownFrameCount(*aFrameCount
);
1601 // If we should start animating right now, do so.
1602 if (mAnimationState
&& aFrameCount
== Some(1u) && mPendingAnimation
&&
1608 // Tell the observers what happened.
1609 image
->mProgressTracker
->SyncNotifyProgress(aProgress
, aInvalidRect
);
1612 void RasterImage::NotifyDecodeComplete(
1613 const DecoderFinalStatus
& aStatus
, const ImageMetadata
& aMetadata
,
1614 const DecoderTelemetry
& aTelemetry
, Progress aProgress
,
1615 const IntRect
& aInvalidRect
, const Maybe
<uint32_t>& aFrameCount
,
1616 DecoderFlags aDecoderFlags
, SurfaceFlags aSurfaceFlags
) {
1617 MOZ_ASSERT(NS_IsMainThread());
1619 // If the decoder detected an error, log it to the error console.
1620 if (aStatus
.mShouldReportError
) {
1621 ReportDecoderError();
1624 // Record all the metadata the decoder gathered about this image.
1625 bool metadataOK
= SetMetadata(aMetadata
, aStatus
.mWasMetadataDecode
);
1627 // This indicates a serious error that requires us to discard all existing
1628 // surfaces and redecode to recover. We'll drop the results from this
1629 // decoder on the floor, since they aren't valid.
1630 RecoverFromInvalidFrames(mSize
, FromSurfaceFlags(aSurfaceFlags
));
1634 MOZ_ASSERT(mError
|| mHasSize
|| !aMetadata
.HasSize(),
1635 "SetMetadata should've gotten a size");
1637 if (!aStatus
.mWasMetadataDecode
&& aStatus
.mFinished
) {
1638 // Flag that we've been decoded before.
1639 mHasBeenDecoded
= true;
1642 // Send out any final notifications.
1643 NotifyProgress(aProgress
, aInvalidRect
, aFrameCount
, aDecoderFlags
,
1646 if (!(aDecoderFlags
& DecoderFlags::FIRST_FRAME_ONLY
) && mHasBeenDecoded
&&
1648 // We've finished a full decode of all animation frames and our
1649 // AnimationState has been notified about them all, so let it know not to
1651 mAnimationState
->NotifyDecodeComplete();
1653 mAnimationState
->UpdateState(mAnimationFinished
, this, mSize
);
1654 if (!rect
.IsEmpty()) {
1655 NotifyProgress(NoProgress
, rect
);
1659 // Do some telemetry if this isn't a metadata decode.
1660 if (!aStatus
.mWasMetadataDecode
) {
1661 if (aTelemetry
.mChunkCount
) {
1662 Telemetry::Accumulate(Telemetry::IMAGE_DECODE_CHUNKS
,
1663 aTelemetry
.mChunkCount
);
1666 if (aStatus
.mFinished
) {
1667 Telemetry::Accumulate(Telemetry::IMAGE_DECODE_TIME
,
1668 int32_t(aTelemetry
.mDecodeTime
.ToMicroseconds()));
1670 if (aTelemetry
.mSpeedHistogram
&& aTelemetry
.mBytesDecoded
) {
1671 Telemetry::Accumulate(*aTelemetry
.mSpeedHistogram
, aTelemetry
.Speed());
1676 // Only act on errors if we have no usable frames from the decoder.
1677 if (aStatus
.mHadError
&&
1678 (!mAnimationState
|| mAnimationState
->KnownFrameCount() == 0)) {
1680 } else if (aStatus
.mWasMetadataDecode
&& !mHasSize
) {
1684 // XXX(aosmond): Can we get this far without mFinished == true?
1685 if (aStatus
.mFinished
&& aStatus
.mWasMetadataDecode
) {
1686 // If we were waiting to fire the load event, go ahead and fire it now.
1687 if (mLoadProgress
) {
1688 NotifyForLoadEvent(*mLoadProgress
);
1689 mLoadProgress
= Nothing();
1692 // If we were a metadata decode and a full decode was requested, do it.
1693 if (mWantFullDecode
) {
1694 mWantFullDecode
= false;
1695 RequestDecodeForSize(mSize
,
1696 DECODE_FLAGS_DEFAULT
| FLAG_HIGH_QUALITY_SCALING
);
1701 void RasterImage::ReportDecoderError() {
1702 nsCOMPtr
<nsIConsoleService
> consoleService
=
1703 do_GetService(NS_CONSOLESERVICE_CONTRACTID
);
1704 nsCOMPtr
<nsIScriptError
> errorObject
=
1705 do_CreateInstance(NS_SCRIPTERROR_CONTRACTID
);
1707 if (consoleService
&& errorObject
) {
1708 nsAutoString
msg(NS_LITERAL_STRING("Image corrupt or truncated."));
1712 if (!GetSpecTruncatedTo1k(uri
)) {
1713 msg
+= NS_LITERAL_STRING(" URI in this note truncated due to length.");
1715 src
= NS_ConvertUTF8toUTF16(uri
);
1717 if (NS_SUCCEEDED(errorObject
->InitWithWindowID(msg
, src
, EmptyString(), 0,
1718 0, nsIScriptError::errorFlag
,
1719 "Image", InnerWindowID()))) {
1720 consoleService
->LogMessage(errorObject
);
1725 already_AddRefed
<imgIContainer
> RasterImage::Unwrap() {
1726 nsCOMPtr
<imgIContainer
> self(this);
1727 return self
.forget();
1730 void RasterImage::PropagateUseCounters(dom::Document
*) {
1734 IntSize
RasterImage::OptimalImageSizeForDest(const gfxSize
& aDest
,
1735 uint32_t aWhichFrame
,
1736 SamplingFilter aSamplingFilter
,
1738 MOZ_ASSERT(aDest
.width
>= 0 || ceil(aDest
.width
) <= INT32_MAX
||
1739 aDest
.height
>= 0 || ceil(aDest
.height
) <= INT32_MAX
,
1740 "Unexpected destination size");
1742 if (mSize
.IsEmpty() || aDest
.IsEmpty()) {
1743 return IntSize(0, 0);
1746 IntSize destSize
= IntSize::Ceil(aDest
.width
, aDest
.height
);
1748 if (aSamplingFilter
== SamplingFilter::GOOD
&&
1749 CanDownscaleDuringDecode(destSize
, aFlags
)) {
1753 // We can't scale to this size. Use our intrinsic size for now.
1757 } // namespace image
1758 } // namespace mozilla