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"
23 #include "LookupResult.h"
24 #include "OrientedImage.h"
25 #include "SourceBuffer.h"
26 #include "SurfaceCache.h"
27 #include "gfx2DGlue.h"
28 #include "gfxContext.h"
29 #include "gfxPlatform.h"
30 #include "mozilla/ClearOnShutdown.h"
31 #include "mozilla/DebugOnly.h"
32 #include "mozilla/Likely.h"
33 #include "mozilla/MemoryReporting.h"
34 #include "mozilla/RefPtr.h"
35 #include "mozilla/SizeOfState.h"
36 #include "mozilla/StaticPrefs_image.h"
37 #include "mozilla/Telemetry.h"
38 #include "mozilla/TimeStamp.h"
39 #include "mozilla/Tuple.h"
40 #include "mozilla/gfx/2D.h"
41 #include "mozilla/gfx/Scale.h"
42 #include "nsComponentManagerUtils.h"
44 #include "nsIConsoleService.h"
45 #include "nsIInputStream.h"
46 #include "nsIScriptError.h"
47 #include "nsISupportsPrimitives.h"
49 #include "nsPresContext.h"
50 #include "nsProperties.h"
53 #include "WindowRenderer.h"
58 using namespace layers
;
66 NS_IMPL_ISUPPORTS(RasterImage
, imgIContainer
)
68 NS_IMPL_ISUPPORTS(RasterImage
, imgIContainer
, imgIContainerDebug
)
71 //******************************************************************************
72 RasterImage::RasterImage(nsIURI
* aURI
/* = nullptr */)
73 : ImageResource(aURI
), // invoke superclass's constructor
76 mDecoderType(DecoderType::UNKNOWN
),
81 mSourceBuffer(MakeNotNull
<SourceBuffer
*>()) {
84 //******************************************************************************
85 RasterImage::~RasterImage() {
86 // Make sure our SourceBuffer is marked as complete. This will ensure that any
87 // outstanding decoders terminate.
88 if (!mSourceBuffer
->IsComplete()) {
89 mSourceBuffer
->Complete(NS_ERROR_ABORT
);
92 // Release all frames from the surface cache.
93 SurfaceCache::RemoveImage(ImageKey(this));
96 Telemetry::Accumulate(Telemetry::IMAGE_DECODE_COUNT
, mDecodeCount
);
99 nsresult
RasterImage::Init(const char* aMimeType
, uint32_t aFlags
) {
100 // We don't support re-initialization
102 return NS_ERROR_ILLEGAL_VALUE
;
105 // Not sure an error can happen before init, but be safe
107 return NS_ERROR_FAILURE
;
110 // We want to avoid redecodes for transient images.
111 MOZ_ASSERT_IF(aFlags
& INIT_FLAG_TRANSIENT
,
112 !(aFlags
& INIT_FLAG_DISCARDABLE
));
114 // Store initialization data
115 StoreDiscardable(!!(aFlags
& INIT_FLAG_DISCARDABLE
));
116 StoreWantFullDecode(!!(aFlags
& INIT_FLAG_DECODE_IMMEDIATELY
));
117 StoreTransient(!!(aFlags
& INIT_FLAG_TRANSIENT
));
118 StoreSyncLoad(!!(aFlags
& INIT_FLAG_SYNC_LOAD
));
120 // Use the MIME type to select a decoder type, and make sure there *is* a
121 // decoder for this MIME type.
122 NS_ENSURE_ARG_POINTER(aMimeType
);
123 mDecoderType
= DecoderFactory::GetDecoderType(aMimeType
);
124 if (mDecoderType
== DecoderType::UNKNOWN
) {
125 return NS_ERROR_FAILURE
;
128 // Lock this image's surfaces in the SurfaceCache if we're not discardable.
129 if (!LoadDiscardable()) {
131 SurfaceCache::LockImage(ImageKey(this));
134 // Mark us as initialized
140 //******************************************************************************
142 RasterImage::RequestRefresh(const TimeStamp
& aTime
) {
143 if (HadRecentRefresh(aTime
)) {
154 if (mAnimationState
) {
155 MOZ_ASSERT(mFrameAnimator
);
156 res
= mFrameAnimator
->RequestRefresh(*mAnimationState
, aTime
);
160 if (res
.mFrameAdvanced
) {
165 // Notify listeners that our frame has actually changed, but do this only
166 // once for all frames that we've now passed (if AdvanceFrame() was called
168 if (!res
.mDirtyRect
.IsEmpty() || res
.mFrameAdvanced
) {
169 auto dirtyRect
= OrientedIntRect::FromUnknownRect(res
.mDirtyRect
);
170 NotifyProgress(NoProgress
, dirtyRect
);
173 if (res
.mAnimationFinished
) {
174 StoreAnimationFinished(true);
179 //******************************************************************************
181 RasterImage::GetWidth(int32_t* aWidth
) {
182 NS_ENSURE_ARG_POINTER(aWidth
);
186 return NS_ERROR_FAILURE
;
189 *aWidth
= mSize
.width
;
193 //******************************************************************************
195 RasterImage::GetHeight(int32_t* aHeight
) {
196 NS_ENSURE_ARG_POINTER(aHeight
);
200 return NS_ERROR_FAILURE
;
203 *aHeight
= mSize
.height
;
207 //******************************************************************************
208 void RasterImage::MediaFeatureValuesChangedAllDocuments(
209 const mozilla::MediaFeatureChange
& aChange
) {}
211 //******************************************************************************
212 nsresult
RasterImage::GetNativeSizes(nsTArray
<IntSize
>& aNativeSizes
) {
214 return NS_ERROR_FAILURE
;
217 aNativeSizes
.Clear();
219 if (mNativeSizes
.IsEmpty()) {
220 aNativeSizes
.AppendElement(mSize
.ToUnknownSize());
222 for (const auto& size
: mNativeSizes
) {
223 aNativeSizes
.AppendElement(size
.ToUnknownSize());
230 //******************************************************************************
231 size_t RasterImage::GetNativeSizesLength() {
232 if (mError
|| !LoadHasSize()) {
236 if (mNativeSizes
.IsEmpty()) {
240 return mNativeSizes
.Length();
243 //******************************************************************************
245 RasterImage::GetIntrinsicSize(nsSize
* aSize
) {
247 return NS_ERROR_FAILURE
;
250 *aSize
= nsSize(nsPresContext::CSSPixelsToAppUnits(mSize
.width
),
251 nsPresContext::CSSPixelsToAppUnits(mSize
.height
));
255 //******************************************************************************
256 Maybe
<AspectRatio
> RasterImage::GetIntrinsicRatio() {
261 return Some(AspectRatio::FromSize(mSize
.width
, mSize
.height
));
264 NS_IMETHODIMP_(Orientation
)
265 RasterImage::GetOrientation() { return mOrientation
; }
267 NS_IMETHODIMP_(Resolution
)
268 RasterImage::GetResolution() { return mResolution
; }
270 //******************************************************************************
272 RasterImage::GetType(uint16_t* aType
) {
273 NS_ENSURE_ARG_POINTER(aType
);
275 *aType
= imgIContainer::TYPE_RASTER
;
280 RasterImage::GetProviderId(uint32_t* aId
) {
281 NS_ENSURE_ARG_POINTER(aId
);
283 *aId
= ImageResource::GetImageProviderId();
287 LookupResult
RasterImage::LookupFrameInternal(const OrientedIntSize
& aSize
,
289 PlaybackType aPlaybackType
,
291 if (mAnimationState
&& aPlaybackType
== PlaybackType::eAnimated
) {
292 MOZ_ASSERT(mFrameAnimator
);
293 MOZ_ASSERT(ToSurfaceFlags(aFlags
) == DefaultSurfaceFlags(),
294 "Can't composite frames with non-default surface flags");
295 return mFrameAnimator
->GetCompositedFrame(*mAnimationState
, aMarkUsed
);
298 SurfaceFlags surfaceFlags
= ToSurfaceFlags(aFlags
);
300 // We don't want any substitution for sync decodes, and substitution would be
301 // illegal when high quality downscaling is disabled, so we use
302 // SurfaceCache::Lookup in this case.
303 if ((aFlags
& FLAG_SYNC_DECODE
) || !(aFlags
& FLAG_HIGH_QUALITY_SCALING
)) {
304 return SurfaceCache::Lookup(
306 RasterSurfaceKey(aSize
.ToUnknownSize(), surfaceFlags
,
307 PlaybackType::eStatic
),
311 // We'll return the best match we can find to the requested frame.
312 return SurfaceCache::LookupBestMatch(
314 RasterSurfaceKey(aSize
.ToUnknownSize(), surfaceFlags
,
315 PlaybackType::eStatic
),
319 LookupResult
RasterImage::LookupFrame(const OrientedIntSize
& aSize
,
321 PlaybackType aPlaybackType
,
323 MOZ_ASSERT(NS_IsMainThread());
325 // If we're opaque, we don't need to care about premultiplied alpha, because
326 // that can only matter for frames with transparency.
328 aFlags
&= ~FLAG_DECODE_NO_PREMULTIPLY_ALPHA
;
331 OrientedIntSize requestedSize
=
332 CanDownscaleDuringDecode(aSize
, aFlags
) ? aSize
: mSize
;
333 if (requestedSize
.IsEmpty()) {
334 // Can't decode to a surface of zero size.
335 return LookupResult(MatchType::NOT_FOUND
);
338 LookupResult result
=
339 LookupFrameInternal(requestedSize
, aFlags
, aPlaybackType
, aMarkUsed
);
341 if (!result
&& !LoadHasSize()) {
342 // We can't request a decode without knowing our intrinsic size. Give up.
343 return LookupResult(MatchType::NOT_FOUND
);
346 // We want to trigger a decode if and only if:
347 // 1) There is no pending decode
348 // 2) There is no acceptable size decoded
349 // 3) The pending decode has not produced a frame yet, a sync decode is
350 // requested, and we have all the source data. Without the source data, we
351 // will just trigger another async decode anyways.
353 // TODO(aosmond): We should better handle case 3. We should actually return
354 // TEMPORARY_ERROR or NOT_READY if we don't have all the source data and a
355 // sync decode is requested. If there is a pending decode and we have all the
356 // source data, we should always be able to block on the frame's monitor --
357 // perhaps this could be accomplished by preallocating the first frame buffer
358 // when we create the decoder.
359 const bool syncDecode
= aFlags
& FLAG_SYNC_DECODE
;
360 const bool avoidRedecode
= aFlags
& FLAG_AVOID_REDECODE_FOR_SIZE
;
361 if (result
.Type() == MatchType::NOT_FOUND
||
362 (result
.Type() == MatchType::SUBSTITUTE_BECAUSE_NOT_FOUND
&&
364 (syncDecode
&& !avoidRedecode
&& !result
&& LoadAllSourceData())) {
365 // We don't have a copy of this frame, and there's no decoder working on
366 // one. (Or we're sync decoding and the existing decoder hasn't even started
367 // yet.) Trigger decoding so it'll be available next time.
368 MOZ_ASSERT(aPlaybackType
!= PlaybackType::eAnimated
||
369 StaticPrefs::image_mem_animated_discardable_AtStartup() ||
370 !mAnimationState
|| mAnimationState
->KnownFrameCount() < 1,
371 "Animated frames should be locked");
373 // The surface cache may suggest the preferred size we are supposed to
374 // decode at. This should only happen if we accept substitutions.
375 if (!result
.SuggestedSize().IsEmpty()) {
376 MOZ_ASSERT(!syncDecode
&& (aFlags
& FLAG_HIGH_QUALITY_SCALING
));
377 requestedSize
= OrientedIntSize::FromUnknownSize(result
.SuggestedSize());
380 bool ranSync
= false, failed
= false;
381 Decode(requestedSize
, aFlags
, aPlaybackType
, ranSync
, failed
);
383 result
.SetFailedToRequestDecode();
386 // If we can or did sync decode, we should already have the frame.
387 if (ranSync
|| syncDecode
) {
389 LookupFrameInternal(requestedSize
, aFlags
, aPlaybackType
, aMarkUsed
);
394 // We still weren't able to get a frame. Give up.
398 // Sync decoding guarantees that we got the frame, but if it's owned by an
399 // async decoder that's currently running, the contents of the frame may not
400 // be available yet. Make sure we get everything.
401 if (LoadAllSourceData() && syncDecode
) {
402 result
.Surface()->WaitUntilFinished();
405 // If we could have done some decoding in this function we need to check if
406 // that decoding encountered an error and hence aborted the surface. We want
407 // to avoid calling IsAborted if we weren't passed any sync decode flag
408 // because IsAborted acquires the monitor for the imgFrame.
409 if (aFlags
& (FLAG_SYNC_DECODE
| FLAG_SYNC_DECODE_IF_FAST
) &&
410 result
.Surface()->IsAborted()) {
411 DrawableSurface tmp
= std::move(result
.Surface());
418 bool RasterImage::IsOpaque() {
423 Progress progress
= mProgressTracker
->GetProgress();
425 // If we haven't yet finished decoding, the safe answer is "not opaque".
426 if (!(progress
& FLAG_DECODE_COMPLETE
)) {
430 // Other, we're opaque if FLAG_HAS_TRANSPARENCY is not set.
431 return !(progress
& FLAG_HAS_TRANSPARENCY
);
435 RasterImage::WillDrawOpaqueNow() {
440 if (mAnimationState
) {
441 if (!StaticPrefs::image_mem_animated_discardable_AtStartup()) {
442 // We never discard frames of animated images.
445 if (mAnimationState
->GetCompositedFrameInvalid()) {
446 // We're not going to draw anything at all.
452 // If we are not locked our decoded data could get discard at any time (ie
453 // between the call to this function and when we are asked to draw), so we
454 // have to return false if we are unlocked.
455 if (mLockCount
== 0) {
459 LookupResult result
= SurfaceCache::LookupBestMatch(
461 RasterSurfaceKey(mSize
.ToUnknownSize(), DefaultSurfaceFlags(),
462 PlaybackType::eStatic
),
463 /* aMarkUsed = */ false);
464 MatchType matchType
= result
.Type();
465 if (matchType
== MatchType::NOT_FOUND
|| matchType
== MatchType::PENDING
||
466 !result
.Surface()->IsFinished()) {
473 void RasterImage::OnSurfaceDiscarded(const SurfaceKey
& aSurfaceKey
) {
474 MOZ_ASSERT(mProgressTracker
);
476 bool animatedFramesDiscarded
=
477 mAnimationState
&& aSurfaceKey
.Playback() == PlaybackType::eAnimated
;
479 nsCOMPtr
<nsIEventTarget
> eventTarget
;
480 if (mProgressTracker
) {
481 eventTarget
= mProgressTracker
->GetEventTarget();
483 eventTarget
= do_GetMainThread();
486 RefPtr
<RasterImage
> image
= this;
487 nsCOMPtr
<nsIRunnable
> ev
=
488 NS_NewRunnableFunction("RasterImage::OnSurfaceDiscarded", [=]() -> void {
489 image
->OnSurfaceDiscardedInternal(animatedFramesDiscarded
);
491 eventTarget
->Dispatch(ev
.forget(), NS_DISPATCH_NORMAL
);
494 void RasterImage::OnSurfaceDiscardedInternal(bool aAnimatedFramesDiscarded
) {
495 MOZ_ASSERT(NS_IsMainThread());
497 if (aAnimatedFramesDiscarded
&& mAnimationState
) {
498 MOZ_ASSERT(StaticPrefs::image_mem_animated_discardable_AtStartup());
500 IntRect rect
= mAnimationState
->UpdateState(this, mSize
.ToUnknownSize());
502 auto dirtyRect
= OrientedIntRect::FromUnknownRect(rect
);
503 NotifyProgress(NoProgress
, dirtyRect
);
506 if (mProgressTracker
) {
507 mProgressTracker
->OnDiscard();
511 //******************************************************************************
513 RasterImage::GetAnimated(bool* aAnimated
) {
515 return NS_ERROR_FAILURE
;
518 NS_ENSURE_ARG_POINTER(aAnimated
);
520 // If we have an AnimationState, we can know for sure.
521 if (mAnimationState
) {
526 // Otherwise, we need to have been decoded to know for sure, since if we were
527 // decoded at least once mAnimationState would have been created for animated
528 // images. This is true even though we check for animation during the
529 // metadata decode, because we may still discover animation only during the
530 // full decode for corrupt images.
531 if (!LoadHasBeenDecoded()) {
532 return NS_ERROR_NOT_AVAILABLE
;
541 //******************************************************************************
542 NS_IMETHODIMP_(int32_t)
543 RasterImage::GetFirstFrameDelay() {
548 bool animated
= false;
549 if (NS_FAILED(GetAnimated(&animated
)) || !animated
) {
553 MOZ_ASSERT(mAnimationState
, "Animated images should have an AnimationState");
554 return mAnimationState
->FirstFrameTimeout().AsEncodedValueDeprecated();
557 NS_IMETHODIMP_(already_AddRefed
<SourceSurface
>)
558 RasterImage::GetFrame(uint32_t aWhichFrame
, uint32_t aFlags
) {
559 return GetFrameAtSize(mSize
.ToUnknownSize(), aWhichFrame
, aFlags
);
562 NS_IMETHODIMP_(already_AddRefed
<SourceSurface
>)
563 RasterImage::GetFrameAtSize(const IntSize
& aSize
, uint32_t aWhichFrame
,
565 MOZ_ASSERT(aWhichFrame
<= FRAME_MAX_VALUE
);
567 AutoProfilerImagePaintMarker
PROFILER_RAII(this);
569 NotifyDrawingObservers();
572 if (aSize
.IsEmpty() || aWhichFrame
> FRAME_MAX_VALUE
|| mError
) {
576 auto size
= OrientedIntSize::FromUnknownSize(aSize
);
578 // Get the frame. If it's not there, it's probably the caller's fault for
579 // not waiting for the data to be loaded from the network or not passing
581 LookupResult result
= LookupFrame(size
, aFlags
, ToPlaybackType(aWhichFrame
),
582 /* aMarkUsed = */ true);
584 // The OS threw this frame away and we couldn't redecode it.
588 return result
.Surface()->GetSourceSurface();
592 RasterImage::IsImageContainerAvailable(WindowRenderer
* aRenderer
,
594 return LoadHasSize();
597 NS_IMETHODIMP_(ImgDrawResult
)
598 RasterImage::GetImageProvider(WindowRenderer
* aRenderer
,
599 const gfx::IntSize
& aSize
,
600 const SVGImageContext
& aSVGContext
,
601 const Maybe
<ImageIntRegion
>& aRegion
,
603 WebRenderImageProvider
** aProvider
) {
604 MOZ_ASSERT(NS_IsMainThread());
605 MOZ_ASSERT(aRenderer
);
608 return ImgDrawResult::BAD_IMAGE
;
611 if (!LoadHasSize()) {
612 return ImgDrawResult::NOT_READY
;
615 if (aSize
.IsEmpty()) {
616 return ImgDrawResult::BAD_ARGS
;
619 // We check the minimum size because while we support downscaling, we do not
620 // support upscaling. If aRequestedSize > mSize, we will never give a larger
621 // surface than mSize. If mSize > aRequestedSize, and mSize > maxTextureSize,
622 // we still want to use image containers if aRequestedSize <= maxTextureSize.
623 int32_t maxTextureSize
= aRenderer
->GetMaxTextureSize();
624 if (min(mSize
.width
, aSize
.width
) > maxTextureSize
||
625 min(mSize
.height
, aSize
.height
) > maxTextureSize
) {
626 return ImgDrawResult::NOT_SUPPORTED
;
629 AutoProfilerImagePaintMarker
PROFILER_RAII(this);
631 NotifyDrawingObservers();
634 // Get the frame. If it's not there, it's probably the caller's fault for
635 // not waiting for the data to be loaded from the network or not passing
637 LookupResult result
= LookupFrame(OrientedIntSize::FromUnknownSize(aSize
),
638 aFlags
, PlaybackType::eAnimated
,
639 /* aMarkUsed = */ true);
641 // The OS threw this frame away and we couldn't redecode it.
642 return ImgDrawResult::NOT_READY
;
645 if (!result
.Surface()->IsFinished()) {
646 result
.Surface().TakeProvider(aProvider
);
647 return ImgDrawResult::INCOMPLETE
;
650 result
.Surface().TakeProvider(aProvider
);
651 switch (result
.Type()) {
652 case MatchType::SUBSTITUTE_BECAUSE_NOT_FOUND
:
653 case MatchType::SUBSTITUTE_BECAUSE_PENDING
:
654 return ImgDrawResult::WRONG_SIZE
;
656 return ImgDrawResult::SUCCESS
;
660 size_t RasterImage::SizeOfSourceWithComputedFallback(
661 SizeOfState
& aState
) const {
662 return mSourceBuffer
->SizeOfIncludingThisWithComputedFallback(
663 aState
.mMallocSizeOf
);
666 bool RasterImage::SetMetadata(const ImageMetadata
& aMetadata
,
667 bool aFromMetadataDecode
) {
668 MOZ_ASSERT(NS_IsMainThread());
674 mResolution
= aMetadata
.GetResolution();
676 if (aMetadata
.HasSize()) {
677 auto metadataSize
= aMetadata
.GetSize();
678 if (metadataSize
.width
< 0 || metadataSize
.height
< 0) {
679 NS_WARNING("Image has negative intrinsic size");
684 MOZ_ASSERT(aMetadata
.HasOrientation());
685 Orientation orientation
= aMetadata
.GetOrientation();
687 // If we already have a size, check the new size against the old one.
689 (metadataSize
!= mSize
|| orientation
!= mOrientation
)) {
691 "Image changed size or orientation on redecode! "
692 "This should not happen!");
697 // Set the size and flag that we have it.
698 mOrientation
= orientation
;
699 mSize
= metadataSize
;
700 mNativeSizes
.Clear();
701 for (const auto& nativeSize
: aMetadata
.GetNativeSizes()) {
702 mNativeSizes
.AppendElement(nativeSize
);
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::max(std::min(hotspot
.x
.value
, mSize
.width
- 1), 0);
748 mHotspot
.y
= std::max(std::min(hotspot
.y
.value
, mSize
.height
- 1), 0);
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
= DefaultDecoderFlags();
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), 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
);
1734 if (!GetSpecTruncatedTo1k(uri
)) {
1735 msg
+= u
" URI in this note truncated due to length."_ns
;
1737 CopyUTF8toUTF16(uri
, src
);
1739 if (NS_SUCCEEDED(errorObject
->InitWithWindowID(msg
, src
, u
""_ns
, 0, 0,
1740 nsIScriptError::errorFlag
,
1741 "Image", InnerWindowID()))) {
1742 consoleService
->LogMessage(errorObject
);
1747 already_AddRefed
<imgIContainer
> RasterImage::Unwrap() {
1748 nsCOMPtr
<imgIContainer
> self(this);
1749 return self
.forget();
1752 void RasterImage::PropagateUseCounters(dom::Document
*) {
1756 IntSize
RasterImage::OptimalImageSizeForDest(const gfxSize
& aDest
,
1757 uint32_t aWhichFrame
,
1758 SamplingFilter aSamplingFilter
,
1760 MOZ_ASSERT(aDest
.width
>= 0 || ceil(aDest
.width
) <= INT32_MAX
||
1761 aDest
.height
>= 0 || ceil(aDest
.height
) <= INT32_MAX
,
1762 "Unexpected destination size");
1764 if (mSize
.IsEmpty() || aDest
.IsEmpty()) {
1765 return IntSize(0, 0);
1768 auto dest
= OrientedIntSize::FromUnknownSize(
1769 IntSize::Ceil(aDest
.width
, aDest
.height
));
1771 if (aSamplingFilter
== SamplingFilter::GOOD
&&
1772 CanDownscaleDuringDecode(dest
, aFlags
)) {
1773 return dest
.ToUnknownSize();
1776 // We can't scale to this size. Use our intrinsic size for now.
1777 return mSize
.ToUnknownSize();
1780 } // namespace image
1781 } // namespace mozilla