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 "RasterImage.h"
15 #include "DecodePool.h"
17 #include "FrameAnimator.h"
18 #include "GeckoProfiler.h"
19 #include "IDecodingTask.h"
20 #include "ImageLogging.h"
21 #include "ImageRegion.h"
22 #include "LookupResult.h"
23 #include "OrientedImage.h"
24 #include "SourceBuffer.h"
25 #include "SurfaceCache.h"
26 #include "gfx2DGlue.h"
27 #include "gfxContext.h"
28 #include "gfxPlatform.h"
29 #include "mozilla/ClearOnShutdown.h"
30 #include "mozilla/DebugOnly.h"
31 #include "mozilla/Likely.h"
32 #include "mozilla/MemoryReporting.h"
33 #include "mozilla/RefPtr.h"
34 #include "mozilla/SizeOfState.h"
35 #include "mozilla/StaticPrefs_image.h"
36 #include "mozilla/Telemetry.h"
37 #include "mozilla/TimeStamp.h"
39 #include "mozilla/gfx/2D.h"
40 #include "nsComponentManagerUtils.h"
42 #include "nsIConsoleService.h"
43 #include "nsIInputStream.h"
44 #include "nsIScriptError.h"
45 #include "nsISupportsPrimitives.h"
47 #include "nsPresContext.h"
48 #include "nsProperties.h"
51 #include "WindowRenderer.h"
56 using namespace layers
;
64 NS_IMPL_ISUPPORTS(RasterImage
, imgIContainer
)
66 NS_IMPL_ISUPPORTS(RasterImage
, imgIContainer
, imgIContainerDebug
)
69 //******************************************************************************
70 RasterImage::RasterImage(nsIURI
* aURI
/* = nullptr */)
71 : ImageResource(aURI
), // invoke superclass's constructor
74 mDecoderType(DecoderType::UNKNOWN
),
79 mSourceBuffer(MakeNotNull
<SourceBuffer
*>()) {
82 //******************************************************************************
83 RasterImage::~RasterImage() {
84 // Make sure our SourceBuffer is marked as complete. This will ensure that any
85 // outstanding decoders terminate.
86 if (!mSourceBuffer
->IsComplete()) {
87 mSourceBuffer
->Complete(NS_ERROR_ABORT
);
90 // Release all frames from the surface cache.
91 SurfaceCache::RemoveImage(ImageKey(this));
94 Telemetry::Accumulate(Telemetry::IMAGE_DECODE_COUNT
, mDecodeCount
);
97 nsresult
RasterImage::Init(const char* aMimeType
, uint32_t aFlags
) {
98 // We don't support re-initialization
100 return NS_ERROR_ILLEGAL_VALUE
;
103 // Not sure an error can happen before init, but be safe
105 return NS_ERROR_FAILURE
;
108 // We want to avoid redecodes for transient images.
109 MOZ_ASSERT_IF(aFlags
& INIT_FLAG_TRANSIENT
,
110 !(aFlags
& INIT_FLAG_DISCARDABLE
));
112 // Store initialization data
113 StoreDiscardable(!!(aFlags
& INIT_FLAG_DISCARDABLE
));
114 StoreWantFullDecode(!!(aFlags
& INIT_FLAG_DECODE_IMMEDIATELY
));
115 StoreTransient(!!(aFlags
& INIT_FLAG_TRANSIENT
));
116 StoreSyncLoad(!!(aFlags
& INIT_FLAG_SYNC_LOAD
));
118 // Use the MIME type to select a decoder type, and make sure there *is* a
119 // decoder for this MIME type.
120 NS_ENSURE_ARG_POINTER(aMimeType
);
121 mDecoderType
= DecoderFactory::GetDecoderType(aMimeType
);
122 if (mDecoderType
== DecoderType::UNKNOWN
) {
123 return NS_ERROR_FAILURE
;
126 // Lock this image's surfaces in the SurfaceCache if we're not discardable.
127 if (!LoadDiscardable()) {
129 SurfaceCache::LockImage(ImageKey(this));
132 // Set the default flags according to the decoder type to allow preferences to
133 // be stored if necessary.
134 mDefaultDecoderFlags
=
135 DecoderFactory::GetDefaultDecoderFlagsForType(mDecoderType
);
137 // Mark us as initialized
143 //******************************************************************************
145 RasterImage::RequestRefresh(const TimeStamp
& aTime
) {
146 if (HadRecentRefresh(aTime
)) {
157 if (mAnimationState
) {
158 MOZ_ASSERT(mFrameAnimator
);
159 res
= mFrameAnimator
->RequestRefresh(*mAnimationState
, aTime
);
163 if (res
.mFrameAdvanced
) {
168 // Notify listeners that our frame has actually changed, but do this only
169 // once for all frames that we've now passed (if AdvanceFrame() was called
171 if (!res
.mDirtyRect
.IsEmpty() || res
.mFrameAdvanced
) {
172 auto dirtyRect
= OrientedIntRect::FromUnknownRect(res
.mDirtyRect
);
173 NotifyProgress(NoProgress
, dirtyRect
);
176 if (res
.mAnimationFinished
) {
177 StoreAnimationFinished(true);
182 //******************************************************************************
184 RasterImage::GetWidth(int32_t* aWidth
) {
185 NS_ENSURE_ARG_POINTER(aWidth
);
189 return NS_ERROR_FAILURE
;
192 *aWidth
= mSize
.width
;
196 //******************************************************************************
198 RasterImage::GetHeight(int32_t* aHeight
) {
199 NS_ENSURE_ARG_POINTER(aHeight
);
203 return NS_ERROR_FAILURE
;
206 *aHeight
= mSize
.height
;
210 //******************************************************************************
211 void RasterImage::MediaFeatureValuesChangedAllDocuments(
212 const mozilla::MediaFeatureChange
& aChange
) {}
214 //******************************************************************************
215 nsresult
RasterImage::GetNativeSizes(nsTArray
<IntSize
>& aNativeSizes
) {
217 return NS_ERROR_FAILURE
;
220 aNativeSizes
.Clear();
222 if (mNativeSizes
.IsEmpty()) {
223 aNativeSizes
.AppendElement(mSize
.ToUnknownSize());
225 for (const auto& size
: mNativeSizes
) {
226 aNativeSizes
.AppendElement(size
.ToUnknownSize());
233 //******************************************************************************
234 size_t RasterImage::GetNativeSizesLength() {
235 if (mError
|| !LoadHasSize()) {
239 if (mNativeSizes
.IsEmpty()) {
243 return mNativeSizes
.Length();
246 //******************************************************************************
248 RasterImage::GetIntrinsicSize(nsSize
* aSize
) {
250 return NS_ERROR_FAILURE
;
253 *aSize
= nsSize(nsPresContext::CSSPixelsToAppUnits(mSize
.width
),
254 nsPresContext::CSSPixelsToAppUnits(mSize
.height
));
258 //******************************************************************************
259 AspectRatio
RasterImage::GetIntrinsicRatio() {
263 return AspectRatio::FromSize(mSize
.width
, mSize
.height
);
266 NS_IMETHODIMP_(Orientation
)
267 RasterImage::GetOrientation() { return mOrientation
; }
269 NS_IMETHODIMP_(Resolution
)
270 RasterImage::GetResolution() { return mResolution
; }
272 //******************************************************************************
274 RasterImage::GetType(uint16_t* aType
) {
275 NS_ENSURE_ARG_POINTER(aType
);
277 *aType
= imgIContainer::TYPE_RASTER
;
282 RasterImage::GetProviderId(uint32_t* aId
) {
283 NS_ENSURE_ARG_POINTER(aId
);
285 *aId
= ImageResource::GetImageProviderId();
289 LookupResult
RasterImage::LookupFrameInternal(const OrientedIntSize
& aSize
,
291 PlaybackType aPlaybackType
,
293 if (mAnimationState
&& aPlaybackType
== PlaybackType::eAnimated
) {
294 MOZ_ASSERT(mFrameAnimator
);
295 MOZ_ASSERT(ToSurfaceFlags(aFlags
) == DefaultSurfaceFlags(),
296 "Can't composite frames with non-default surface flags");
297 return mFrameAnimator
->GetCompositedFrame(*mAnimationState
, aMarkUsed
);
300 SurfaceFlags surfaceFlags
= ToSurfaceFlags(aFlags
);
302 // We don't want any substitution for sync decodes, and substitution would be
303 // illegal when high quality downscaling is disabled, so we use
304 // SurfaceCache::Lookup in this case.
305 if ((aFlags
& FLAG_SYNC_DECODE
) || !(aFlags
& FLAG_HIGH_QUALITY_SCALING
)) {
306 return SurfaceCache::Lookup(
308 RasterSurfaceKey(aSize
.ToUnknownSize(), surfaceFlags
,
309 PlaybackType::eStatic
),
313 // We'll return the best match we can find to the requested frame.
314 return SurfaceCache::LookupBestMatch(
316 RasterSurfaceKey(aSize
.ToUnknownSize(), surfaceFlags
,
317 PlaybackType::eStatic
),
321 LookupResult
RasterImage::LookupFrame(const OrientedIntSize
& aSize
,
323 PlaybackType aPlaybackType
,
325 MOZ_ASSERT(NS_IsMainThread());
327 // If we're opaque, we don't need to care about premultiplied alpha, because
328 // that can only matter for frames with transparency.
330 aFlags
&= ~FLAG_DECODE_NO_PREMULTIPLY_ALPHA
;
333 OrientedIntSize requestedSize
=
334 CanDownscaleDuringDecode(aSize
, aFlags
) ? aSize
: mSize
;
335 if (requestedSize
.IsEmpty()) {
336 // Can't decode to a surface of zero size.
337 return LookupResult(MatchType::NOT_FOUND
);
340 LookupResult result
=
341 LookupFrameInternal(requestedSize
, aFlags
, aPlaybackType
, aMarkUsed
);
343 if (!result
&& !LoadHasSize()) {
344 // We can't request a decode without knowing our intrinsic size. Give up.
345 return LookupResult(MatchType::NOT_FOUND
);
348 // We want to trigger a decode if and only if:
349 // 1) There is no pending decode
350 // 2) There is no acceptable size decoded
351 // 3) The pending decode has not produced a frame yet, a sync decode is
352 // requested, and we have all the source data. Without the source data, we
353 // will just trigger another async decode anyways.
355 // TODO(aosmond): We should better handle case 3. We should actually return
356 // TEMPORARY_ERROR or NOT_READY if we don't have all the source data and a
357 // sync decode is requested. If there is a pending decode and we have all the
358 // source data, we should always be able to block on the frame's monitor --
359 // perhaps this could be accomplished by preallocating the first frame buffer
360 // when we create the decoder.
361 const bool syncDecode
= aFlags
& FLAG_SYNC_DECODE
;
362 const bool avoidRedecode
= aFlags
& FLAG_AVOID_REDECODE_FOR_SIZE
;
363 if (result
.Type() == MatchType::NOT_FOUND
||
364 (result
.Type() == MatchType::SUBSTITUTE_BECAUSE_NOT_FOUND
&&
366 (syncDecode
&& !avoidRedecode
&& !result
&& LoadAllSourceData())) {
367 // We don't have a copy of this frame, and there's no decoder working on
368 // one. (Or we're sync decoding and the existing decoder hasn't even started
369 // yet.) Trigger decoding so it'll be available next time.
370 MOZ_ASSERT(aPlaybackType
!= PlaybackType::eAnimated
||
371 StaticPrefs::image_mem_animated_discardable_AtStartup() ||
372 !mAnimationState
|| mAnimationState
->KnownFrameCount() < 1,
373 "Animated frames should be locked");
375 // The surface cache may suggest the preferred size we are supposed to
376 // decode at. This should only happen if we accept substitutions.
377 if (!result
.SuggestedSize().IsEmpty()) {
378 MOZ_ASSERT(!syncDecode
&& (aFlags
& FLAG_HIGH_QUALITY_SCALING
));
379 requestedSize
= OrientedIntSize::FromUnknownSize(result
.SuggestedSize());
382 bool ranSync
= false, failed
= false;
383 Decode(requestedSize
, aFlags
, aPlaybackType
, ranSync
, failed
);
385 result
.SetFailedToRequestDecode();
388 // If we can or did sync decode, we should already have the frame.
389 if (ranSync
|| syncDecode
) {
391 LookupFrameInternal(requestedSize
, aFlags
, aPlaybackType
, aMarkUsed
);
396 // We still weren't able to get a frame. Give up.
400 // Sync decoding guarantees that we got the frame, but if it's owned by an
401 // async decoder that's currently running, the contents of the frame may not
402 // be available yet. Make sure we get everything.
403 if (LoadAllSourceData() && syncDecode
) {
404 result
.Surface()->WaitUntilFinished();
407 // If we could have done some decoding in this function we need to check if
408 // that decoding encountered an error and hence aborted the surface. We want
409 // to avoid calling IsAborted if we weren't passed any sync decode flag
410 // because IsAborted acquires the monitor for the imgFrame.
411 if (aFlags
& (FLAG_SYNC_DECODE
| FLAG_SYNC_DECODE_IF_FAST
) &&
412 result
.Surface()->IsAborted()) {
413 DrawableSurface tmp
= std::move(result
.Surface());
420 bool RasterImage::IsOpaque() {
425 Progress progress
= mProgressTracker
->GetProgress();
427 // If we haven't yet finished decoding, the safe answer is "not opaque".
428 if (!(progress
& FLAG_DECODE_COMPLETE
)) {
432 // Other, we're opaque if FLAG_HAS_TRANSPARENCY is not set.
433 return !(progress
& FLAG_HAS_TRANSPARENCY
);
437 RasterImage::WillDrawOpaqueNow() {
442 if (mAnimationState
) {
443 if (!StaticPrefs::image_mem_animated_discardable_AtStartup()) {
444 // We never discard frames of animated images.
447 if (mAnimationState
->GetCompositedFrameInvalid()) {
448 // We're not going to draw anything at all.
454 // If we are not locked our decoded data could get discard at any time (ie
455 // between the call to this function and when we are asked to draw), so we
456 // have to return false if we are unlocked.
457 if (mLockCount
== 0) {
461 LookupResult result
= SurfaceCache::LookupBestMatch(
463 RasterSurfaceKey(mSize
.ToUnknownSize(), DefaultSurfaceFlags(),
464 PlaybackType::eStatic
),
465 /* aMarkUsed = */ false);
466 MatchType matchType
= result
.Type();
467 if (matchType
== MatchType::NOT_FOUND
|| matchType
== MatchType::PENDING
||
468 !result
.Surface()->IsFinished()) {
475 void RasterImage::OnSurfaceDiscarded(const SurfaceKey
& aSurfaceKey
) {
476 MOZ_ASSERT(mProgressTracker
);
478 bool animatedFramesDiscarded
=
479 mAnimationState
&& aSurfaceKey
.Playback() == PlaybackType::eAnimated
;
481 nsCOMPtr
<nsIEventTarget
> eventTarget
= do_GetMainThread();
483 RefPtr
<RasterImage
> image
= this;
484 nsCOMPtr
<nsIRunnable
> ev
=
485 NS_NewRunnableFunction("RasterImage::OnSurfaceDiscarded", [=]() -> void {
486 image
->OnSurfaceDiscardedInternal(animatedFramesDiscarded
);
488 eventTarget
->Dispatch(ev
.forget(), NS_DISPATCH_NORMAL
);
491 void RasterImage::OnSurfaceDiscardedInternal(bool aAnimatedFramesDiscarded
) {
492 MOZ_ASSERT(NS_IsMainThread());
494 if (aAnimatedFramesDiscarded
&& mAnimationState
) {
495 MOZ_ASSERT(StaticPrefs::image_mem_animated_discardable_AtStartup());
497 IntRect rect
= mAnimationState
->UpdateState(this, mSize
.ToUnknownSize());
499 auto dirtyRect
= OrientedIntRect::FromUnknownRect(rect
);
500 NotifyProgress(NoProgress
, dirtyRect
);
503 if (mProgressTracker
) {
504 mProgressTracker
->OnDiscard();
508 //******************************************************************************
510 RasterImage::GetAnimated(bool* aAnimated
) {
512 return NS_ERROR_FAILURE
;
515 NS_ENSURE_ARG_POINTER(aAnimated
);
517 // If we have an AnimationState, we can know for sure.
518 if (mAnimationState
) {
523 // Otherwise, we need to have been decoded to know for sure, since if we were
524 // decoded at least once mAnimationState would have been created for animated
525 // images. This is true even though we check for animation during the
526 // metadata decode, because we may still discover animation only during the
527 // full decode for corrupt images.
528 if (!LoadHasBeenDecoded()) {
529 return NS_ERROR_NOT_AVAILABLE
;
538 //******************************************************************************
539 NS_IMETHODIMP_(int32_t)
540 RasterImage::GetFirstFrameDelay() {
545 bool animated
= false;
546 if (NS_FAILED(GetAnimated(&animated
)) || !animated
) {
550 MOZ_ASSERT(mAnimationState
, "Animated images should have an AnimationState");
551 return mAnimationState
->FirstFrameTimeout().AsEncodedValueDeprecated();
554 NS_IMETHODIMP_(already_AddRefed
<SourceSurface
>)
555 RasterImage::GetFrame(uint32_t aWhichFrame
, uint32_t aFlags
) {
556 return GetFrameAtSize(mSize
.ToUnknownSize(), aWhichFrame
, aFlags
);
559 NS_IMETHODIMP_(already_AddRefed
<SourceSurface
>)
560 RasterImage::GetFrameAtSize(const IntSize
& aSize
, uint32_t aWhichFrame
,
562 MOZ_ASSERT(aWhichFrame
<= FRAME_MAX_VALUE
);
564 AutoProfilerImagePaintMarker
PROFILER_RAII(this);
566 NotifyDrawingObservers();
569 if (aSize
.IsEmpty() || aWhichFrame
> FRAME_MAX_VALUE
|| mError
) {
573 auto size
= OrientedIntSize::FromUnknownSize(aSize
);
575 // Get the frame. If it's not there, it's probably the caller's fault for
576 // not waiting for the data to be loaded from the network or not passing
578 LookupResult result
= LookupFrame(size
, aFlags
, ToPlaybackType(aWhichFrame
),
579 /* aMarkUsed = */ true);
581 // The OS threw this frame away and we couldn't redecode it.
585 return result
.Surface()->GetSourceSurface();
589 RasterImage::IsImageContainerAvailable(WindowRenderer
* aRenderer
,
591 return LoadHasSize();
594 NS_IMETHODIMP_(ImgDrawResult
)
595 RasterImage::GetImageProvider(WindowRenderer
* aRenderer
,
596 const gfx::IntSize
& aSize
,
597 const SVGImageContext
& aSVGContext
,
598 const Maybe
<ImageIntRegion
>& aRegion
,
600 WebRenderImageProvider
** aProvider
) {
601 MOZ_ASSERT(NS_IsMainThread());
602 MOZ_ASSERT(aRenderer
);
605 return ImgDrawResult::BAD_IMAGE
;
608 if (!LoadHasSize()) {
609 return ImgDrawResult::NOT_READY
;
612 if (aSize
.IsEmpty()) {
613 return ImgDrawResult::BAD_ARGS
;
616 // We check the minimum size because while we support downscaling, we do not
617 // support upscaling. If aRequestedSize > mSize, we will never give a larger
618 // surface than mSize. If mSize > aRequestedSize, and mSize > maxTextureSize,
619 // we still want to use image containers if aRequestedSize <= maxTextureSize.
620 int32_t maxTextureSize
= aRenderer
->GetMaxTextureSize();
621 if (min(mSize
.width
, aSize
.width
) > maxTextureSize
||
622 min(mSize
.height
, aSize
.height
) > maxTextureSize
) {
623 return ImgDrawResult::NOT_SUPPORTED
;
626 AutoProfilerImagePaintMarker
PROFILER_RAII(this);
628 NotifyDrawingObservers();
631 // Get the frame. If it's not there, it's probably the caller's fault for
632 // not waiting for the data to be loaded from the network or not passing
634 LookupResult result
= LookupFrame(OrientedIntSize::FromUnknownSize(aSize
),
635 aFlags
, PlaybackType::eAnimated
,
636 /* aMarkUsed = */ true);
638 // The OS threw this frame away and we couldn't redecode it.
639 return ImgDrawResult::NOT_READY
;
642 if (!result
.Surface()->IsFinished()) {
643 result
.Surface().TakeProvider(aProvider
);
644 return ImgDrawResult::INCOMPLETE
;
647 result
.Surface().TakeProvider(aProvider
);
648 switch (result
.Type()) {
649 case MatchType::SUBSTITUTE_BECAUSE_NOT_FOUND
:
650 case MatchType::SUBSTITUTE_BECAUSE_PENDING
:
651 return ImgDrawResult::WRONG_SIZE
;
653 return ImgDrawResult::SUCCESS
;
657 size_t RasterImage::SizeOfSourceWithComputedFallback(
658 SizeOfState
& aState
) const {
659 return mSourceBuffer
->SizeOfIncludingThisWithComputedFallback(
660 aState
.mMallocSizeOf
);
663 bool RasterImage::SetMetadata(const ImageMetadata
& aMetadata
,
664 bool aFromMetadataDecode
) {
665 MOZ_ASSERT(NS_IsMainThread());
671 mResolution
= aMetadata
.GetResolution();
673 if (aMetadata
.HasSize()) {
674 auto metadataSize
= aMetadata
.GetSize();
675 if (metadataSize
.width
< 0 || metadataSize
.height
< 0) {
676 NS_WARNING("Image has negative intrinsic size");
681 MOZ_ASSERT(aMetadata
.HasOrientation());
682 Orientation orientation
= aMetadata
.GetOrientation();
684 // If we already have a size, check the new size against the old one.
686 (metadataSize
!= mSize
|| orientation
!= mOrientation
)) {
688 "Image changed size or orientation on redecode! "
689 "This should not happen!");
694 // Set the size and flag that we have it.
695 mOrientation
= orientation
;
696 mSize
= metadataSize
;
697 mNativeSizes
.Clear();
698 for (const auto& nativeSize
: aMetadata
.GetNativeSizes()) {
699 mNativeSizes
.AppendElement(nativeSize
);
704 MOZ_ASSERT_IF(mAnimationState
&& !aFromMetadataDecode
,
705 mAnimationState
->LoopCount() == aMetadata
.GetLoopCount());
707 if (LoadHasSize() && aMetadata
.HasAnimation() && !mAnimationState
) {
708 // We're becoming animated, so initialize animation stuff.
709 mAnimationState
.emplace(mAnimationMode
);
710 mFrameAnimator
= MakeUnique
<FrameAnimator
>(this, mSize
.ToUnknownSize());
712 if (!StaticPrefs::image_mem_animated_discardable_AtStartup()) {
713 // We don't support discarding animated images (See bug 414259).
714 // Lock the image and throw away the key.
718 if (!aFromMetadataDecode
) {
719 // The metadata decode reported that this image isn't animated, but we
720 // discovered that it actually was during the full decode. This is a
721 // rare failure that only occurs for corrupt images. To recover, we need
722 // to discard all existing surfaces and redecode.
727 if (mAnimationState
) {
728 mAnimationState
->SetLoopCount(aMetadata
.GetLoopCount());
729 mAnimationState
->SetFirstFrameTimeout(aMetadata
.GetFirstFrameTimeout());
731 if (aMetadata
.HasLoopLength()) {
732 mAnimationState
->SetLoopLength(aMetadata
.GetLoopLength());
734 if (aMetadata
.HasFirstFrameRefreshArea()) {
735 mAnimationState
->SetFirstFrameRefreshArea(
736 aMetadata
.GetFirstFrameRefreshArea());
740 if (aMetadata
.HasHotspot()) {
741 // NOTE(heycam): We shouldn't have any image formats that support both
742 // orientation and hotspots, so we assert that rather than add code
743 // to orient the hotspot point correctly.
744 MOZ_ASSERT(mOrientation
.IsIdentity(), "Would need to orient hotspot point");
746 auto hotspot
= aMetadata
.GetHotspot();
747 mHotspot
.x
= std::clamp(hotspot
.x
.value
, 0, mSize
.width
- 1);
748 mHotspot
.y
= std::clamp(hotspot
.y
.value
, 0, mSize
.height
- 1);
755 RasterImage::SetAnimationMode(uint16_t aAnimationMode
) {
756 if (mAnimationState
) {
757 mAnimationState
->SetAnimationMode(aAnimationMode
);
759 return SetAnimationModeInternal(aAnimationMode
);
762 //******************************************************************************
764 nsresult
RasterImage::StartAnimation() {
766 return NS_ERROR_FAILURE
;
769 MOZ_ASSERT(ShouldAnimate(), "Should not animate!");
771 // If we're not ready to animate, then set mPendingAnimation, which will cause
772 // us to start animating if and when we do become ready.
773 StorePendingAnimation(!mAnimationState
||
774 mAnimationState
->KnownFrameCount() < 1);
775 if (LoadPendingAnimation()) {
779 // Don't bother to animate if we're displaying the first frame forever.
780 if (mAnimationState
->GetCurrentAnimationFrameIndex() == 0 &&
781 mAnimationState
->FirstFrameTimeout() == FrameTimeout::Forever()) {
782 StoreAnimationFinished(true);
783 return NS_ERROR_ABORT
;
786 // We need to set the time that this initial frame was first displayed, as
787 // this is used in AdvanceFrame().
788 mAnimationState
->InitAnimationFrameTimeIfNecessary();
793 //******************************************************************************
794 nsresult
RasterImage::StopAnimation() {
795 MOZ_ASSERT(mAnimating
, "Should be animating!");
799 rv
= NS_ERROR_FAILURE
;
801 mAnimationState
->SetAnimationFrameTime(TimeStamp());
808 //******************************************************************************
810 RasterImage::ResetAnimation() {
812 return NS_ERROR_FAILURE
;
815 StorePendingAnimation(false);
817 if (mAnimationMode
== kDontAnimMode
|| !mAnimationState
||
818 mAnimationState
->GetCurrentAnimationFrameIndex() == 0) {
822 StoreAnimationFinished(false);
828 MOZ_ASSERT(mAnimationState
, "Should have AnimationState");
829 MOZ_ASSERT(mFrameAnimator
, "Should have FrameAnimator");
830 mFrameAnimator
->ResetAnimation(*mAnimationState
);
832 IntRect area
= mAnimationState
->FirstFrameRefreshArea();
833 NotifyProgress(NoProgress
, OrientedIntRect::FromUnknownRect(area
));
835 // Start the animation again. It may not have been running before, if
836 // mAnimationFinished was true before entering this function.
842 //******************************************************************************
844 RasterImage::SetAnimationStartTime(const TimeStamp
& aTime
) {
845 if (mError
|| mAnimationMode
== kDontAnimMode
|| mAnimating
||
850 mAnimationState
->SetAnimationFrameTime(aTime
);
853 NS_IMETHODIMP_(float)
854 RasterImage::GetFrameIndex(uint32_t aWhichFrame
) {
855 MOZ_ASSERT(aWhichFrame
<= FRAME_MAX_VALUE
, "Invalid argument");
856 return (aWhichFrame
== FRAME_FIRST
|| !mAnimationState
)
858 : mAnimationState
->GetCurrentAnimationFrameIndex();
861 NS_IMETHODIMP_(IntRect
)
862 RasterImage::GetImageSpaceInvalidationRect(const IntRect
& aRect
) {
863 // Note that we do not transform aRect into an UnorientedIntRect, since
864 // RasterImage::NotifyProgress notifies all consumers of the image using
865 // OrientedIntRect values. (This is unlike OrientedImage, which notifies
866 // using inner image coordinates.)
870 nsresult
RasterImage::OnImageDataComplete(nsIRequest
*, nsresult aStatus
,
872 MOZ_ASSERT(NS_IsMainThread());
874 // Record that we have all the data we're going to get now.
875 StoreAllSourceData(true);
877 // Let decoders know that there won't be any more data coming.
878 mSourceBuffer
->Complete(aStatus
);
880 // Allow a synchronous metadata decode if mSyncLoad was set, or if we're
881 // running on a single thread (in which case waiting for the async metadata
882 // decoder could delay this image's load event quite a bit), or if this image
884 bool canSyncDecodeMetadata
=
885 LoadSyncLoad() || LoadTransient() || DecodePool::NumberOfCores() < 2;
887 if (canSyncDecodeMetadata
&& !LoadHasSize()) {
888 // We're loading this image synchronously, so it needs to be usable after
889 // this call returns. Since we haven't gotten our size yet, we need to do a
890 // synchronous metadata decode here.
891 DecodeMetadata(FLAG_SYNC_DECODE
);
894 // Determine our final status, giving precedence to Necko failure codes. We
895 // check after running the metadata decode in case it triggered an error.
896 nsresult finalStatus
= mError
? NS_ERROR_FAILURE
: NS_OK
;
897 if (NS_FAILED(aStatus
)) {
898 finalStatus
= aStatus
;
901 // If loading failed, report an error.
902 if (NS_FAILED(finalStatus
)) {
906 Progress loadProgress
= LoadCompleteProgress(aLastPart
, mError
, finalStatus
);
908 if (!LoadHasSize() && !mError
) {
909 // We don't have our size yet, so we'll fire the load event in SetSize().
910 MOZ_ASSERT(!canSyncDecodeMetadata
,
911 "Firing load async after metadata sync decode?");
912 mLoadProgress
= Some(loadProgress
);
916 NotifyForLoadEvent(loadProgress
);
921 void RasterImage::NotifyForLoadEvent(Progress aProgress
) {
922 MOZ_ASSERT(LoadHasSize() || mError
,
923 "Need to know size before firing load event");
925 !LoadHasSize() || (mProgressTracker
->GetProgress() & FLAG_SIZE_AVAILABLE
),
926 "Should have notified that the size is available if we have it");
928 // If we encountered an error, make sure we notify for that as well.
930 aProgress
|= FLAG_HAS_ERROR
;
933 // Notify our listeners, which will fire this image's load event.
934 NotifyProgress(aProgress
);
937 nsresult
RasterImage::OnImageDataAvailable(nsIRequest
*,
938 nsIInputStream
* aInputStream
,
939 uint64_t, uint32_t aCount
) {
940 nsresult rv
= mSourceBuffer
->AppendFromInputStream(aInputStream
, aCount
);
941 if (NS_SUCCEEDED(rv
) && !LoadSomeSourceData()) {
942 StoreSomeSourceData(true);
943 if (!LoadSyncLoad()) {
944 // Create an async metadata decoder and verify we succeed in doing so.
945 rv
= DecodeMetadata(DECODE_FLAGS_DEFAULT
);
955 nsresult
RasterImage::SetSourceSizeHint(uint32_t aSizeHint
) {
956 if (aSizeHint
== 0) {
960 nsresult rv
= mSourceBuffer
->ExpectLength(aSizeHint
);
961 if (rv
== NS_ERROR_OUT_OF_MEMORY
) {
962 // Flush memory, try to get some back, and try again.
963 rv
= nsMemory::HeapMinimize(true);
964 if (NS_SUCCEEDED(rv
)) {
965 rv
= mSourceBuffer
->ExpectLength(aSizeHint
);
972 nsresult
RasterImage::GetHotspotX(int32_t* aX
) {
977 nsresult
RasterImage::GetHotspotY(int32_t* aY
) {
982 void RasterImage::Discard() {
983 MOZ_ASSERT(NS_IsMainThread());
984 MOZ_ASSERT(CanDiscard(), "Asked to discard but can't");
985 MOZ_ASSERT(!mAnimationState
||
986 StaticPrefs::image_mem_animated_discardable_AtStartup(),
987 "Asked to discard for animated image");
989 // Delete all the decoded frames.
990 SurfaceCache::RemoveImage(ImageKey(this));
992 if (mAnimationState
) {
993 IntRect rect
= mAnimationState
->UpdateState(this, mSize
.ToUnknownSize());
995 auto dirtyRect
= OrientedIntRect::FromUnknownRect(rect
);
996 NotifyProgress(NoProgress
, dirtyRect
);
999 // Notify that we discarded.
1000 if (mProgressTracker
) {
1001 mProgressTracker
->OnDiscard();
1005 bool RasterImage::CanDiscard() {
1006 return LoadAllSourceData() &&
1007 // Can discard animated images if the pref is set
1008 (!mAnimationState
||
1009 StaticPrefs::image_mem_animated_discardable_AtStartup());
1013 RasterImage::StartDecoding(uint32_t aFlags
, uint32_t aWhichFrame
) {
1015 return NS_ERROR_FAILURE
;
1018 if (!LoadHasSize()) {
1019 StoreWantFullDecode(true);
1023 uint32_t flags
= (aFlags
& FLAG_ASYNC_NOTIFY
) | FLAG_SYNC_DECODE_IF_FAST
|
1024 FLAG_HIGH_QUALITY_SCALING
;
1025 return RequestDecodeForSize(mSize
.ToUnknownSize(), flags
, aWhichFrame
);
1028 bool RasterImage::StartDecodingWithResult(uint32_t aFlags
,
1029 uint32_t aWhichFrame
) {
1034 if (!LoadHasSize()) {
1035 StoreWantFullDecode(true);
1039 uint32_t flags
= (aFlags
& FLAG_ASYNC_NOTIFY
) | FLAG_SYNC_DECODE_IF_FAST
|
1040 FLAG_HIGH_QUALITY_SCALING
;
1041 LookupResult result
= RequestDecodeForSizeInternal(mSize
, flags
, aWhichFrame
);
1042 DrawableSurface surface
= std::move(result
.Surface());
1043 return surface
&& surface
->IsFinished();
1046 bool RasterImage::HasDecodedPixels() {
1047 LookupResult result
= SurfaceCache::LookupBestMatch(
1049 RasterSurfaceKey(mSize
.ToUnknownSize(), DefaultSurfaceFlags(),
1050 PlaybackType::eStatic
),
1051 /* aMarkUsed = */ false);
1052 MatchType matchType
= result
.Type();
1053 if (matchType
== MatchType::NOT_FOUND
|| matchType
== MatchType::PENDING
||
1054 !bool(result
.Surface())) {
1058 return !result
.Surface()->GetDecodedRect().IsEmpty();
1061 imgIContainer::DecodeResult
RasterImage::RequestDecodeWithResult(
1062 uint32_t aFlags
, uint32_t aWhichFrame
) {
1063 MOZ_ASSERT(NS_IsMainThread());
1066 return imgIContainer::DECODE_REQUEST_FAILED
;
1069 uint32_t flags
= aFlags
| FLAG_ASYNC_NOTIFY
;
1070 LookupResult result
= RequestDecodeForSizeInternal(mSize
, flags
, aWhichFrame
);
1071 DrawableSurface surface
= std::move(result
.Surface());
1072 if (surface
&& surface
->IsFinished()) {
1073 return imgIContainer::DECODE_SURFACE_AVAILABLE
;
1075 if (result
.GetFailedToRequestDecode()) {
1076 return imgIContainer::DECODE_REQUEST_FAILED
;
1078 return imgIContainer::DECODE_REQUESTED
;
1082 RasterImage::RequestDecodeForSize(const IntSize
& aSize
, uint32_t aFlags
,
1083 uint32_t aWhichFrame
) {
1084 MOZ_ASSERT(NS_IsMainThread());
1087 return NS_ERROR_FAILURE
;
1090 RequestDecodeForSizeInternal(OrientedIntSize::FromUnknownSize(aSize
), aFlags
,
1096 LookupResult
RasterImage::RequestDecodeForSizeInternal(
1097 const OrientedIntSize
& aSize
, uint32_t aFlags
, uint32_t aWhichFrame
) {
1098 MOZ_ASSERT(NS_IsMainThread());
1100 if (aWhichFrame
> FRAME_MAX_VALUE
) {
1101 return LookupResult(MatchType::NOT_FOUND
);
1105 LookupResult result
= LookupResult(MatchType::NOT_FOUND
);
1106 result
.SetFailedToRequestDecode();
1110 if (!LoadHasSize()) {
1111 StoreWantFullDecode(true);
1112 return LookupResult(MatchType::NOT_FOUND
);
1115 // Decide whether to sync decode images we can decode quickly. Here we are
1116 // explicitly trading off flashing for responsiveness in the case that we're
1117 // redecoding an image (see bug 845147).
1118 bool shouldSyncDecodeIfFast
=
1119 !LoadHasBeenDecoded() && (aFlags
& FLAG_SYNC_DECODE_IF_FAST
);
1122 shouldSyncDecodeIfFast
? aFlags
: aFlags
& ~FLAG_SYNC_DECODE_IF_FAST
;
1124 // Perform a frame lookup, which will implicitly start decoding if needed.
1125 return LookupFrame(aSize
, flags
, ToPlaybackType(aWhichFrame
),
1126 /* aMarkUsed = */ false);
1129 static bool LaunchDecodingTask(IDecodingTask
* aTask
, RasterImage
* aImage
,
1130 uint32_t aFlags
, bool aHaveSourceData
) {
1131 if (aHaveSourceData
) {
1132 nsCString
uri(aImage
->GetURIString());
1134 // If we have all the data, we can sync decode if requested.
1135 if (aFlags
& imgIContainer::FLAG_SYNC_DECODE
) {
1136 DecodePool::Singleton()->SyncRunIfPossible(aTask
, uri
);
1140 if (aFlags
& imgIContainer::FLAG_SYNC_DECODE_IF_FAST
) {
1141 return DecodePool::Singleton()->SyncRunIfPreferred(aTask
, uri
);
1145 // Perform an async decode. We also take this path if we don't have all the
1146 // source data yet, since sync decoding is impossible in that situation.
1147 DecodePool::Singleton()->AsyncRun(aTask
);
1151 void RasterImage::Decode(const OrientedIntSize
& aSize
, uint32_t aFlags
,
1152 PlaybackType aPlaybackType
, bool& aOutRanSync
,
1154 MOZ_ASSERT(NS_IsMainThread());
1161 // If we don't have a size yet, we can't do any other decoding.
1162 if (!LoadHasSize()) {
1163 StoreWantFullDecode(true);
1167 // We're about to decode again, which may mean that some of the previous sizes
1168 // we've decoded at aren't useful anymore. We can allow them to expire from
1169 // the cache by unlocking them here. When the decode finishes, it will send an
1170 // invalidation that will cause all instances of this image to redraw. If this
1171 // image is locked, any surfaces that are still useful will become locked
1172 // again when LookupFrame touches them, and the remainder will eventually
1174 SurfaceCache::UnlockEntries(ImageKey(this));
1176 // Determine which flags we need to decode this image with.
1177 DecoderFlags decoderFlags
= mDefaultDecoderFlags
;
1178 if (aFlags
& FLAG_ASYNC_NOTIFY
) {
1179 decoderFlags
|= DecoderFlags::ASYNC_NOTIFY
;
1181 if (LoadTransient()) {
1182 decoderFlags
|= DecoderFlags::IMAGE_IS_TRANSIENT
;
1184 if (LoadHasBeenDecoded()) {
1185 decoderFlags
|= DecoderFlags::IS_REDECODE
;
1187 if ((aFlags
& FLAG_SYNC_DECODE
) || !(aFlags
& FLAG_HIGH_QUALITY_SCALING
)) {
1188 // Used SurfaceCache::Lookup instead of SurfaceCache::LookupBestMatch. That
1189 // means the caller can handle a differently sized surface to be returned
1191 decoderFlags
|= DecoderFlags::CANNOT_SUBSTITUTE
;
1194 SurfaceFlags surfaceFlags
= ToSurfaceFlags(aFlags
);
1196 // If there's no transparency, it doesn't matter whether we premultiply
1198 surfaceFlags
&= ~SurfaceFlags::NO_PREMULTIPLY_ALPHA
;
1201 // Create a decoder.
1202 RefPtr
<IDecodingTask
> task
;
1204 bool animated
= mAnimationState
&& aPlaybackType
== PlaybackType::eAnimated
;
1206 size_t currentFrame
= mAnimationState
->GetCurrentAnimationFrameIndex();
1207 rv
= DecoderFactory::CreateAnimationDecoder(
1208 mDecoderType
, WrapNotNull(this), mSourceBuffer
, mSize
.ToUnknownSize(),
1209 decoderFlags
, surfaceFlags
, currentFrame
, getter_AddRefs(task
));
1211 rv
= DecoderFactory::CreateDecoder(mDecoderType
, WrapNotNull(this),
1212 mSourceBuffer
, mSize
.ToUnknownSize(),
1213 aSize
.ToUnknownSize(), decoderFlags
,
1214 surfaceFlags
, getter_AddRefs(task
));
1217 if (rv
== NS_ERROR_ALREADY_INITIALIZED
) {
1218 // We raced with an already pending decoder, and it finished before we
1219 // managed to insert the new decoder. Pretend we did a sync call to make
1220 // the caller lookup in the surface cache again.
1227 // We pass false for aAllowInvalidation because we may be asked to use
1228 // async notifications. Any potential invalidation here will be sent when
1229 // RequestRefresh is called, or NotifyDecodeComplete.
1233 mAnimationState
->UpdateState(this, mSize
.ToUnknownSize(), false);
1234 MOZ_ASSERT(rect
.IsEmpty());
1237 // Make sure DecoderFactory was able to create a decoder successfully.
1238 if (NS_FAILED(rv
)) {
1247 // We're ready to decode; start the decoder.
1248 aOutRanSync
= LaunchDecodingTask(task
, this, aFlags
, LoadAllSourceData());
1252 RasterImage::DecodeMetadata(uint32_t aFlags
) {
1254 return NS_ERROR_FAILURE
;
1257 MOZ_ASSERT(!LoadHasSize(), "Should not do unnecessary metadata decodes");
1259 // Create a decoder.
1260 RefPtr
<IDecodingTask
> task
= DecoderFactory::CreateMetadataDecoder(
1261 mDecoderType
, WrapNotNull(this), mDefaultDecoderFlags
, mSourceBuffer
);
1263 // Make sure DecoderFactory was able to create a decoder successfully.
1265 return NS_ERROR_FAILURE
;
1268 // We're ready to decode; start the decoder.
1269 LaunchDecodingTask(task
, this, aFlags
, LoadAllSourceData());
1273 void RasterImage::RecoverFromInvalidFrames(const OrientedIntSize
& aSize
,
1275 if (!LoadHasSize()) {
1279 NS_WARNING("A RasterImage's frames became invalid. Attempting to recover...");
1281 // Discard all existing frames, since they're probably all now invalid.
1282 SurfaceCache::RemoveImage(ImageKey(this));
1284 // Relock the image if it's supposed to be locked.
1285 if (mLockCount
> 0) {
1286 SurfaceCache::LockImage(ImageKey(this));
1289 bool unused1
, unused2
;
1291 // Animated images require some special handling, because we normally require
1292 // that they never be discarded.
1293 if (mAnimationState
) {
1294 Decode(mSize
, aFlags
| FLAG_SYNC_DECODE
, PlaybackType::eAnimated
, unused1
,
1300 // For non-animated images, it's fine to recover using an async decode.
1301 Decode(aSize
, aFlags
, PlaybackType::eStatic
, unused1
, unused2
);
1304 bool RasterImage::CanDownscaleDuringDecode(const OrientedIntSize
& aSize
,
1306 // Check basic requirements: downscale-during-decode is enabled, Skia is
1307 // available, this image isn't transient, we have all the source data and know
1308 // our size, and the flags allow us to do it.
1309 if (!LoadHasSize() || LoadTransient() ||
1310 !StaticPrefs::image_downscale_during_decode_enabled() ||
1311 !(aFlags
& imgIContainer::FLAG_HIGH_QUALITY_SCALING
)) {
1315 // We don't downscale animated images during decode.
1316 if (mAnimationState
) {
1321 if (aSize
.width
>= mSize
.width
|| aSize
.height
>= mSize
.height
) {
1325 // Zero or negative width or height is unacceptable.
1326 if (aSize
.width
< 1 || aSize
.height
< 1) {
1330 // There's no point in scaling if we can't store the result.
1331 if (!SurfaceCache::CanHold(aSize
.ToUnknownSize())) {
1338 ImgDrawResult
RasterImage::DrawInternal(DrawableSurface
&& aSurface
,
1339 gfxContext
* aContext
,
1340 const OrientedIntSize
& aSize
,
1341 const ImageRegion
& aRegion
,
1342 SamplingFilter aSamplingFilter
,
1343 uint32_t aFlags
, float aOpacity
) {
1344 gfxContextMatrixAutoSaveRestore
saveMatrix(aContext
);
1345 ImageRegion
region(aRegion
);
1346 bool frameIsFinished
= aSurface
->IsFinished();
1348 AutoProfilerImagePaintMarker
PROFILER_RAII(this);
1350 NotifyDrawingObservers();
1353 // By now we may have a frame with the requested size. If not, we need to
1354 // adjust the drawing parameters accordingly.
1355 IntSize finalSize
= aSurface
->GetSize();
1356 bool couldRedecodeForBetterFrame
= false;
1357 if (finalSize
!= aSize
.ToUnknownSize()) {
1358 gfx::MatrixScales
scale(double(aSize
.width
) / finalSize
.width
,
1359 double(aSize
.height
) / finalSize
.height
);
1360 aContext
->Multiply(gfx::Matrix::Scaling(scale
));
1361 region
.Scale(1.0 / scale
.xScale
, 1.0 / scale
.yScale
);
1363 couldRedecodeForBetterFrame
= CanDownscaleDuringDecode(aSize
, aFlags
);
1366 if (!aSurface
->Draw(aContext
, region
, aSamplingFilter
, aFlags
, aOpacity
)) {
1367 RecoverFromInvalidFrames(aSize
, aFlags
);
1368 return ImgDrawResult::TEMPORARY_ERROR
;
1370 if (!frameIsFinished
) {
1371 return ImgDrawResult::INCOMPLETE
;
1373 if (couldRedecodeForBetterFrame
) {
1374 return ImgDrawResult::WRONG_SIZE
;
1376 return ImgDrawResult::SUCCESS
;
1379 //******************************************************************************
1380 NS_IMETHODIMP_(ImgDrawResult
)
1381 RasterImage::Draw(gfxContext
* aContext
, const IntSize
& aSize
,
1382 const ImageRegion
& aRegion
, uint32_t aWhichFrame
,
1383 SamplingFilter aSamplingFilter
,
1384 const SVGImageContext
& /*aSVGContext - ignored*/,
1385 uint32_t aFlags
, float aOpacity
) {
1386 if (aWhichFrame
> FRAME_MAX_VALUE
) {
1387 return ImgDrawResult::BAD_ARGS
;
1391 return ImgDrawResult::BAD_IMAGE
;
1394 // Illegal -- you can't draw with non-default decode flags.
1395 // (Disabling colorspace conversion might make sense to allow, but
1396 // we don't currently.)
1397 if (ToSurfaceFlags(aFlags
) != DefaultSurfaceFlags()) {
1398 return ImgDrawResult::BAD_ARGS
;
1402 return ImgDrawResult::BAD_ARGS
;
1405 if (mAnimationConsumers
== 0 && mAnimationState
) {
1406 SendOnUnlockedDraw(aFlags
);
1409 // If we're not using SamplingFilter::GOOD, we shouldn't high-quality scale or
1410 // downscale during decode.
1411 uint32_t flags
= aSamplingFilter
== SamplingFilter::GOOD
1413 : aFlags
& ~FLAG_HIGH_QUALITY_SCALING
;
1415 auto size
= OrientedIntSize::FromUnknownSize(aSize
);
1416 LookupResult result
= LookupFrame(size
, 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 ImgDrawResult drawResult
=
1430 DrawInternal(std::move(result
.Surface()), aContext
, size
, aRegion
,
1431 aSamplingFilter
, flags
, aOpacity
);
1433 if (shouldRecordTelemetry
) {
1434 TimeDuration drawLatency
= TimeStamp::Now() - mDrawStartTime
;
1435 Telemetry::Accumulate(Telemetry::IMAGE_DECODE_ON_DRAW_LATENCY
,
1436 int32_t(drawLatency
.ToMicroseconds()));
1437 mDrawStartTime
= TimeStamp();
1443 //******************************************************************************
1446 RasterImage::LockImage() {
1447 MOZ_ASSERT(NS_IsMainThread(),
1448 "Main thread to encourage serialization with UnlockImage");
1450 return NS_ERROR_FAILURE
;
1453 // Increment the lock count
1456 // Lock this image's surfaces in the SurfaceCache.
1457 if (mLockCount
== 1) {
1458 SurfaceCache::LockImage(ImageKey(this));
1464 //******************************************************************************
1467 RasterImage::UnlockImage() {
1468 MOZ_ASSERT(NS_IsMainThread(),
1469 "Main thread to encourage serialization with LockImage");
1471 return NS_ERROR_FAILURE
;
1474 // It's an error to call this function if the lock count is 0
1475 MOZ_ASSERT(mLockCount
> 0, "Calling UnlockImage with mLockCount == 0!");
1476 if (mLockCount
== 0) {
1477 return NS_ERROR_ABORT
;
1480 // Decrement our lock count
1483 // Unlock this image's surfaces in the SurfaceCache.
1484 if (mLockCount
== 0) {
1485 SurfaceCache::UnlockImage(ImageKey(this));
1491 //******************************************************************************
1494 RasterImage::RequestDiscard() {
1495 if (LoadDiscardable() && // Enabled at creation time...
1496 mLockCount
== 0 && // ...not temporarily disabled...
1504 // Idempotent error flagging routine. If a decoder is open, shuts it down.
1505 void RasterImage::DoError() {
1506 // If we've flagged an error before, we have nothing to do
1511 // We can't safely handle errors off-main-thread, so dispatch a worker to
1513 if (!NS_IsMainThread()) {
1514 HandleErrorWorker::DispatchIfNeeded(this);
1518 // Put the container in an error state.
1521 // Stop animation and release our FrameAnimator.
1525 mAnimationState
= Nothing();
1526 mFrameAnimator
= nullptr;
1528 // Release all locks.
1530 SurfaceCache::UnlockImage(ImageKey(this));
1532 // Release all frames from the surface cache.
1533 SurfaceCache::RemoveImage(ImageKey(this));
1535 // Invalidate to get rid of any partially-drawn image content.
1536 auto dirtyRect
= OrientedIntRect({0, 0}, mSize
);
1537 NotifyProgress(NoProgress
, dirtyRect
);
1539 MOZ_LOG(gImgLog
, LogLevel::Error
,
1540 ("RasterImage: [this=%p] Error detected for image\n", this));
1544 void RasterImage::HandleErrorWorker::DispatchIfNeeded(RasterImage
* aImage
) {
1545 RefPtr
<HandleErrorWorker
> worker
= new HandleErrorWorker(aImage
);
1546 NS_DispatchToMainThread(worker
);
1549 RasterImage::HandleErrorWorker::HandleErrorWorker(RasterImage
* aImage
)
1550 : Runnable("image::RasterImage::HandleErrorWorker"), mImage(aImage
) {
1551 MOZ_ASSERT(mImage
, "Should have image");
1555 RasterImage::HandleErrorWorker::Run() {
1561 bool RasterImage::ShouldAnimate() {
1562 return ImageResource::ShouldAnimate() && mAnimationState
&&
1563 mAnimationState
->KnownFrameCount() >= 1 && !LoadAnimationFinished();
1568 RasterImage::GetFramesNotified(uint32_t* aFramesNotified
) {
1569 NS_ENSURE_ARG_POINTER(aFramesNotified
);
1571 *aFramesNotified
= mFramesNotified
;
1577 void RasterImage::NotifyProgress(
1579 const OrientedIntRect
& aInvalidRect
/* = OrientedIntRect() */,
1580 const Maybe
<uint32_t>& aFrameCount
/* = Nothing() */,
1581 DecoderFlags aDecoderFlags
/* = DefaultDecoderFlags() */,
1582 SurfaceFlags aSurfaceFlags
/* = DefaultSurfaceFlags() */) {
1583 MOZ_ASSERT(NS_IsMainThread());
1585 // Ensure that we stay alive long enough to finish notifying.
1586 RefPtr
<RasterImage
> image
= this;
1588 OrientedIntRect invalidRect
= aInvalidRect
;
1590 if (!(aDecoderFlags
& DecoderFlags::FIRST_FRAME_ONLY
)) {
1591 // We may have decoded new animation frames; update our animation state.
1592 MOZ_ASSERT_IF(aFrameCount
&& *aFrameCount
> 1, mAnimationState
|| mError
);
1593 if (mAnimationState
&& aFrameCount
) {
1594 mAnimationState
->UpdateKnownFrameCount(*aFrameCount
);
1597 // If we should start animating right now, do so.
1598 if (mAnimationState
&& aFrameCount
== Some(1u) && LoadPendingAnimation() &&
1603 if (mAnimationState
) {
1604 IntRect rect
= mAnimationState
->UpdateState(this, mSize
.ToUnknownSize());
1606 invalidRect
.UnionRect(invalidRect
,
1607 OrientedIntRect::FromUnknownRect(rect
));
1611 // Tell the observers what happened.
1612 image
->mProgressTracker
->SyncNotifyProgress(aProgress
,
1613 invalidRect
.ToUnknownRect());
1616 void RasterImage::NotifyDecodeComplete(
1617 const DecoderFinalStatus
& aStatus
, const ImageMetadata
& aMetadata
,
1618 const DecoderTelemetry
& aTelemetry
, Progress aProgress
,
1619 const OrientedIntRect
& aInvalidRect
, const Maybe
<uint32_t>& aFrameCount
,
1620 DecoderFlags aDecoderFlags
, SurfaceFlags aSurfaceFlags
) {
1621 MOZ_ASSERT(NS_IsMainThread());
1623 // If the decoder detected an error, log it to the error console.
1624 if (aStatus
.mShouldReportError
) {
1625 ReportDecoderError();
1628 // Record all the metadata the decoder gathered about this image.
1629 bool metadataOK
= SetMetadata(aMetadata
, aStatus
.mWasMetadataDecode
);
1631 // This indicates a serious error that requires us to discard all existing
1632 // surfaces and redecode to recover. We'll drop the results from this
1633 // decoder on the floor, since they aren't valid.
1634 RecoverFromInvalidFrames(mSize
, FromSurfaceFlags(aSurfaceFlags
));
1638 MOZ_ASSERT(mError
|| LoadHasSize() || !aMetadata
.HasSize(),
1639 "SetMetadata should've gotten a size");
1641 if (!aStatus
.mWasMetadataDecode
&& aStatus
.mFinished
) {
1642 // Flag that we've been decoded before.
1643 StoreHasBeenDecoded(true);
1646 // Send out any final notifications.
1647 NotifyProgress(aProgress
, aInvalidRect
, aFrameCount
, aDecoderFlags
,
1650 if (!(aDecoderFlags
& DecoderFlags::FIRST_FRAME_ONLY
)) {
1651 // We may have decoded new animation frames; update our animation state.
1652 MOZ_ASSERT_IF(aFrameCount
&& *aFrameCount
> 1, mAnimationState
|| mError
);
1653 if (mAnimationState
&& aFrameCount
) {
1654 mAnimationState
->UpdateKnownFrameCount(*aFrameCount
);
1657 // If we should start animating right now, do so.
1658 if (mAnimationState
&& aFrameCount
== Some(1u) && LoadPendingAnimation() &&
1663 if (mAnimationState
&& LoadHasBeenDecoded()) {
1664 // We've finished a full decode of all animation frames and our
1665 // AnimationState has been notified about them all, so let it know not to
1667 mAnimationState
->NotifyDecodeComplete();
1669 IntRect rect
= mAnimationState
->UpdateState(this, mSize
.ToUnknownSize());
1671 if (!rect
.IsEmpty()) {
1672 auto dirtyRect
= OrientedIntRect::FromUnknownRect(rect
);
1673 NotifyProgress(NoProgress
, dirtyRect
);
1678 // Do some telemetry if this isn't a metadata decode.
1679 if (!aStatus
.mWasMetadataDecode
) {
1680 if (aTelemetry
.mChunkCount
) {
1681 Telemetry::Accumulate(Telemetry::IMAGE_DECODE_CHUNKS
,
1682 aTelemetry
.mChunkCount
);
1685 if (aStatus
.mFinished
) {
1686 Telemetry::Accumulate(Telemetry::IMAGE_DECODE_TIME
,
1687 int32_t(aTelemetry
.mDecodeTime
.ToMicroseconds()));
1689 if (aTelemetry
.mSpeedHistogram
&& aTelemetry
.mBytesDecoded
) {
1690 Telemetry::Accumulate(*aTelemetry
.mSpeedHistogram
, aTelemetry
.Speed());
1695 // Only act on errors if we have no usable frames from the decoder.
1696 if (aStatus
.mHadError
&&
1697 (!mAnimationState
|| mAnimationState
->KnownFrameCount() == 0)) {
1699 } else if (aStatus
.mWasMetadataDecode
&& !LoadHasSize()) {
1703 // XXX(aosmond): Can we get this far without mFinished == true?
1704 if (aStatus
.mFinished
&& aStatus
.mWasMetadataDecode
) {
1705 // If we were waiting to fire the load event, go ahead and fire it now.
1706 if (mLoadProgress
) {
1707 NotifyForLoadEvent(*mLoadProgress
);
1708 mLoadProgress
= Nothing();
1711 // If we were a metadata decode and a full decode was requested, do it.
1712 if (LoadWantFullDecode()) {
1713 StoreWantFullDecode(false);
1714 RequestDecodeForSizeInternal(mSize
,
1715 DECODE_FLAGS_DEFAULT
|
1716 FLAG_HIGH_QUALITY_SCALING
|
1717 FLAG_AVOID_REDECODE_FOR_SIZE
,
1723 void RasterImage::ReportDecoderError() {
1724 nsCOMPtr
<nsIConsoleService
> consoleService
=
1725 do_GetService(NS_CONSOLESERVICE_CONTRACTID
);
1726 nsCOMPtr
<nsIScriptError
> errorObject
=
1727 do_CreateInstance(NS_SCRIPTERROR_CONTRACTID
);
1729 if (consoleService
&& errorObject
) {
1730 nsAutoString
msg(u
"Image corrupt or truncated."_ns
);
1733 if (!GetSpecTruncatedTo1k(src
)) {
1734 msg
+= u
" URI in this note truncated due to length."_ns
;
1737 if (NS_SUCCEEDED(errorObject
->InitWithWindowID(msg
, src
, 0, 0,
1738 nsIScriptError::errorFlag
,
1739 "Image", InnerWindowID()))) {
1740 consoleService
->LogMessage(errorObject
);
1745 already_AddRefed
<imgIContainer
> RasterImage::Unwrap() {
1746 nsCOMPtr
<imgIContainer
> self(this);
1747 return self
.forget();
1750 void RasterImage::PropagateUseCounters(dom::Document
*) {
1754 IntSize
RasterImage::OptimalImageSizeForDest(const gfxSize
& aDest
,
1755 uint32_t aWhichFrame
,
1756 SamplingFilter aSamplingFilter
,
1758 MOZ_ASSERT(aDest
.width
>= 0 || ceil(aDest
.width
) <= INT32_MAX
||
1759 aDest
.height
>= 0 || ceil(aDest
.height
) <= INT32_MAX
,
1760 "Unexpected destination size");
1762 if (mSize
.IsEmpty() || aDest
.IsEmpty()) {
1763 return IntSize(0, 0);
1766 auto dest
= OrientedIntSize::FromUnknownSize(
1767 IntSize::Ceil(aDest
.width
, aDest
.height
));
1769 if (aSamplingFilter
== SamplingFilter::GOOD
&&
1770 CanDownscaleDuringDecode(dest
, aFlags
)) {
1771 return dest
.ToUnknownSize();
1774 // We can't scale to this size. Use our intrinsic size for now.
1775 return mSize
.ToUnknownSize();
1778 } // namespace image
1779 } // namespace mozilla