Bug 1767595 [wpt PR 33926] - Added a WPT-test for connecting a TMST to a video elemen...
[gecko.git] / image / RasterImage.cpp
blob0857ffefdbbce53b020ceb9d91fe2e8e03d6eeb4
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"
10 #include <stdint.h>
12 #include <algorithm>
13 #include <utility>
15 #include "DecodePool.h"
16 #include "Decoder.h"
17 #include "FrameAnimator.h"
18 #include "GeckoProfiler.h"
19 #include "IDecodingTask.h"
20 #include "ImageLogging.h"
21 #include "ImageRegion.h"
22 #include "Layers.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"
43 #include "nsError.h"
44 #include "nsIConsoleService.h"
45 #include "nsIInputStream.h"
46 #include "nsIScriptError.h"
47 #include "nsISupportsPrimitives.h"
48 #include "nsMemory.h"
49 #include "nsPresContext.h"
50 #include "nsProperties.h"
51 #include "prenv.h"
52 #include "prsystem.h"
53 #include "WindowRenderer.h"
55 namespace mozilla {
57 using namespace gfx;
58 using namespace layers;
60 namespace image {
62 using std::ceil;
63 using std::min;
65 #ifndef DEBUG
66 NS_IMPL_ISUPPORTS(RasterImage, imgIContainer)
67 #else
68 NS_IMPL_ISUPPORTS(RasterImage, imgIContainer, imgIContainerDebug)
69 #endif
71 //******************************************************************************
72 RasterImage::RasterImage(nsIURI* aURI /* = nullptr */)
73 : ImageResource(aURI), // invoke superclass's constructor
74 mSize(0, 0),
75 mLockCount(0),
76 mDecoderType(DecoderType::UNKNOWN),
77 mDecodeCount(0),
78 #ifdef DEBUG
79 mFramesNotified(0),
80 #endif
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));
95 // Record Telemetry.
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
101 if (mInitialized) {
102 return NS_ERROR_ILLEGAL_VALUE;
105 // Not sure an error can happen before init, but be safe
106 if (mError) {
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()) {
130 mLockCount++;
131 SurfaceCache::LockImage(ImageKey(this));
134 // Mark us as initialized
135 mInitialized = true;
137 return NS_OK;
140 //******************************************************************************
141 NS_IMETHODIMP_(void)
142 RasterImage::RequestRefresh(const TimeStamp& aTime) {
143 if (HadRecentRefresh(aTime)) {
144 return;
147 EvaluateAnimation();
149 if (!mAnimating) {
150 return;
153 RefreshResult res;
154 if (mAnimationState) {
155 MOZ_ASSERT(mFrameAnimator);
156 res = mFrameAnimator->RequestRefresh(*mAnimationState, aTime);
159 #ifdef DEBUG
160 if (res.mFrameAdvanced) {
161 mFramesNotified++;
163 #endif
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
167 // more than once).
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);
175 EvaluateAnimation();
179 //******************************************************************************
180 NS_IMETHODIMP
181 RasterImage::GetWidth(int32_t* aWidth) {
182 NS_ENSURE_ARG_POINTER(aWidth);
184 if (mError) {
185 *aWidth = 0;
186 return NS_ERROR_FAILURE;
189 *aWidth = mSize.width;
190 return NS_OK;
193 //******************************************************************************
194 NS_IMETHODIMP
195 RasterImage::GetHeight(int32_t* aHeight) {
196 NS_ENSURE_ARG_POINTER(aHeight);
198 if (mError) {
199 *aHeight = 0;
200 return NS_ERROR_FAILURE;
203 *aHeight = mSize.height;
204 return NS_OK;
207 //******************************************************************************
208 nsresult RasterImage::GetNativeSizes(nsTArray<IntSize>& aNativeSizes) const {
209 if (mError) {
210 return NS_ERROR_FAILURE;
213 aNativeSizes.Clear();
215 if (mNativeSizes.IsEmpty()) {
216 aNativeSizes.AppendElement(mSize.ToUnknownSize());
217 } else {
218 for (const auto& size : mNativeSizes) {
219 aNativeSizes.AppendElement(size.ToUnknownSize());
223 return NS_OK;
226 //******************************************************************************
227 size_t RasterImage::GetNativeSizesLength() const {
228 if (mError || !LoadHasSize()) {
229 return 0;
232 if (mNativeSizes.IsEmpty()) {
233 return 1;
236 return mNativeSizes.Length();
239 //******************************************************************************
240 NS_IMETHODIMP
241 RasterImage::GetIntrinsicSize(nsSize* aSize) {
242 if (mError) {
243 return NS_ERROR_FAILURE;
246 *aSize = nsSize(nsPresContext::CSSPixelsToAppUnits(mSize.width),
247 nsPresContext::CSSPixelsToAppUnits(mSize.height));
248 return NS_OK;
251 //******************************************************************************
252 Maybe<AspectRatio> RasterImage::GetIntrinsicRatio() {
253 if (mError) {
254 return Nothing();
257 return Some(AspectRatio::FromSize(mSize.width, mSize.height));
260 NS_IMETHODIMP_(Orientation)
261 RasterImage::GetOrientation() { return mOrientation; }
263 NS_IMETHODIMP_(Resolution)
264 RasterImage::GetResolution() { return mResolution; }
266 //******************************************************************************
267 NS_IMETHODIMP
268 RasterImage::GetType(uint16_t* aType) {
269 NS_ENSURE_ARG_POINTER(aType);
271 *aType = imgIContainer::TYPE_RASTER;
272 return NS_OK;
275 NS_IMETHODIMP
276 RasterImage::GetProviderId(uint32_t* aId) {
277 NS_ENSURE_ARG_POINTER(aId);
279 *aId = ImageResource::GetImageProviderId();
280 return NS_OK;
283 LookupResult RasterImage::LookupFrameInternal(const OrientedIntSize& aSize,
284 uint32_t aFlags,
285 PlaybackType aPlaybackType,
286 bool aMarkUsed) {
287 if (mAnimationState && aPlaybackType == PlaybackType::eAnimated) {
288 MOZ_ASSERT(mFrameAnimator);
289 MOZ_ASSERT(ToSurfaceFlags(aFlags) == DefaultSurfaceFlags(),
290 "Can't composite frames with non-default surface flags");
291 return mFrameAnimator->GetCompositedFrame(*mAnimationState, aMarkUsed);
294 SurfaceFlags surfaceFlags = ToSurfaceFlags(aFlags);
296 // We don't want any substitution for sync decodes, and substitution would be
297 // illegal when high quality downscaling is disabled, so we use
298 // SurfaceCache::Lookup in this case.
299 if ((aFlags & FLAG_SYNC_DECODE) || !(aFlags & FLAG_HIGH_QUALITY_SCALING)) {
300 return SurfaceCache::Lookup(
301 ImageKey(this),
302 RasterSurfaceKey(aSize.ToUnknownSize(), surfaceFlags,
303 PlaybackType::eStatic),
304 aMarkUsed);
307 // We'll return the best match we can find to the requested frame.
308 return SurfaceCache::LookupBestMatch(
309 ImageKey(this),
310 RasterSurfaceKey(aSize.ToUnknownSize(), surfaceFlags,
311 PlaybackType::eStatic),
312 aMarkUsed);
315 LookupResult RasterImage::LookupFrame(const OrientedIntSize& aSize,
316 uint32_t aFlags,
317 PlaybackType aPlaybackType,
318 bool aMarkUsed) {
319 MOZ_ASSERT(NS_IsMainThread());
321 // If we're opaque, we don't need to care about premultiplied alpha, because
322 // that can only matter for frames with transparency.
323 if (IsOpaque()) {
324 aFlags &= ~FLAG_DECODE_NO_PREMULTIPLY_ALPHA;
327 OrientedIntSize requestedSize =
328 CanDownscaleDuringDecode(aSize, aFlags) ? aSize : mSize;
329 if (requestedSize.IsEmpty()) {
330 // Can't decode to a surface of zero size.
331 return LookupResult(MatchType::NOT_FOUND);
334 LookupResult result =
335 LookupFrameInternal(requestedSize, aFlags, aPlaybackType, aMarkUsed);
337 if (!result && !LoadHasSize()) {
338 // We can't request a decode without knowing our intrinsic size. Give up.
339 return LookupResult(MatchType::NOT_FOUND);
342 const bool syncDecode = aFlags & FLAG_SYNC_DECODE;
343 const bool avoidRedecode = aFlags & FLAG_AVOID_REDECODE_FOR_SIZE;
344 if (result.Type() == MatchType::NOT_FOUND ||
345 (result.Type() == MatchType::SUBSTITUTE_BECAUSE_NOT_FOUND &&
346 !avoidRedecode) ||
347 (syncDecode && !avoidRedecode && !result)) {
348 // We don't have a copy of this frame, and there's no decoder working on
349 // one. (Or we're sync decoding and the existing decoder hasn't even started
350 // yet.) Trigger decoding so it'll be available next time.
351 MOZ_ASSERT(aPlaybackType != PlaybackType::eAnimated ||
352 StaticPrefs::image_mem_animated_discardable_AtStartup() ||
353 !mAnimationState || mAnimationState->KnownFrameCount() < 1,
354 "Animated frames should be locked");
356 // The surface cache may suggest the preferred size we are supposed to
357 // decode at. This should only happen if we accept substitutions.
358 if (!result.SuggestedSize().IsEmpty()) {
359 MOZ_ASSERT(!syncDecode && (aFlags & FLAG_HIGH_QUALITY_SCALING));
360 requestedSize = OrientedIntSize::FromUnknownSize(result.SuggestedSize());
363 bool ranSync = false, failed = false;
364 Decode(requestedSize, aFlags, aPlaybackType, ranSync, failed);
365 if (failed) {
366 result.SetFailedToRequestDecode();
369 // If we can or did sync decode, we should already have the frame.
370 if (ranSync || syncDecode) {
371 result =
372 LookupFrameInternal(requestedSize, aFlags, aPlaybackType, aMarkUsed);
376 if (!result) {
377 // We still weren't able to get a frame. Give up.
378 return result;
381 // Sync decoding guarantees that we got the frame, but if it's owned by an
382 // async decoder that's currently running, the contents of the frame may not
383 // be available yet. Make sure we get everything.
384 if (LoadAllSourceData() && syncDecode) {
385 result.Surface()->WaitUntilFinished();
388 // If we could have done some decoding in this function we need to check if
389 // that decoding encountered an error and hence aborted the surface. We want
390 // to avoid calling IsAborted if we weren't passed any sync decode flag
391 // because IsAborted acquires the monitor for the imgFrame.
392 if (aFlags & (FLAG_SYNC_DECODE | FLAG_SYNC_DECODE_IF_FAST) &&
393 result.Surface()->IsAborted()) {
394 DrawableSurface tmp = std::move(result.Surface());
395 return result;
398 return result;
401 bool RasterImage::IsOpaque() {
402 if (mError) {
403 return false;
406 Progress progress = mProgressTracker->GetProgress();
408 // If we haven't yet finished decoding, the safe answer is "not opaque".
409 if (!(progress & FLAG_DECODE_COMPLETE)) {
410 return false;
413 // Other, we're opaque if FLAG_HAS_TRANSPARENCY is not set.
414 return !(progress & FLAG_HAS_TRANSPARENCY);
417 NS_IMETHODIMP_(bool)
418 RasterImage::WillDrawOpaqueNow() {
419 if (!IsOpaque()) {
420 return false;
423 if (mAnimationState) {
424 if (!StaticPrefs::image_mem_animated_discardable_AtStartup()) {
425 // We never discard frames of animated images.
426 return true;
427 } else {
428 if (mAnimationState->GetCompositedFrameInvalid()) {
429 // We're not going to draw anything at all.
430 return false;
435 // If we are not locked our decoded data could get discard at any time (ie
436 // between the call to this function and when we are asked to draw), so we
437 // have to return false if we are unlocked.
438 if (mLockCount == 0) {
439 return false;
442 LookupResult result = SurfaceCache::LookupBestMatch(
443 ImageKey(this),
444 RasterSurfaceKey(mSize.ToUnknownSize(), DefaultSurfaceFlags(),
445 PlaybackType::eStatic),
446 /* aMarkUsed = */ false);
447 MatchType matchType = result.Type();
448 if (matchType == MatchType::NOT_FOUND || matchType == MatchType::PENDING ||
449 !result.Surface()->IsFinished()) {
450 return false;
453 return true;
456 void RasterImage::OnSurfaceDiscarded(const SurfaceKey& aSurfaceKey) {
457 MOZ_ASSERT(mProgressTracker);
459 bool animatedFramesDiscarded =
460 mAnimationState && aSurfaceKey.Playback() == PlaybackType::eAnimated;
462 nsCOMPtr<nsIEventTarget> eventTarget;
463 if (mProgressTracker) {
464 eventTarget = mProgressTracker->GetEventTarget();
465 } else {
466 eventTarget = do_GetMainThread();
469 RefPtr<RasterImage> image = this;
470 nsCOMPtr<nsIRunnable> ev =
471 NS_NewRunnableFunction("RasterImage::OnSurfaceDiscarded", [=]() -> void {
472 image->OnSurfaceDiscardedInternal(animatedFramesDiscarded);
474 eventTarget->Dispatch(ev.forget(), NS_DISPATCH_NORMAL);
477 void RasterImage::OnSurfaceDiscardedInternal(bool aAnimatedFramesDiscarded) {
478 MOZ_ASSERT(NS_IsMainThread());
480 if (aAnimatedFramesDiscarded && mAnimationState) {
481 MOZ_ASSERT(StaticPrefs::image_mem_animated_discardable_AtStartup());
483 IntRect rect = mAnimationState->UpdateState(this, mSize.ToUnknownSize());
485 auto dirtyRect = OrientedIntRect::FromUnknownRect(rect);
486 NotifyProgress(NoProgress, dirtyRect);
489 if (mProgressTracker) {
490 mProgressTracker->OnDiscard();
494 //******************************************************************************
495 NS_IMETHODIMP
496 RasterImage::GetAnimated(bool* aAnimated) {
497 if (mError) {
498 return NS_ERROR_FAILURE;
501 NS_ENSURE_ARG_POINTER(aAnimated);
503 // If we have an AnimationState, we can know for sure.
504 if (mAnimationState) {
505 *aAnimated = true;
506 return NS_OK;
509 // Otherwise, we need to have been decoded to know for sure, since if we were
510 // decoded at least once mAnimationState would have been created for animated
511 // images. This is true even though we check for animation during the
512 // metadata decode, because we may still discover animation only during the
513 // full decode for corrupt images.
514 if (!LoadHasBeenDecoded()) {
515 return NS_ERROR_NOT_AVAILABLE;
518 // We know for sure
519 *aAnimated = false;
521 return NS_OK;
524 //******************************************************************************
525 NS_IMETHODIMP_(int32_t)
526 RasterImage::GetFirstFrameDelay() {
527 if (mError) {
528 return -1;
531 bool animated = false;
532 if (NS_FAILED(GetAnimated(&animated)) || !animated) {
533 return -1;
536 MOZ_ASSERT(mAnimationState, "Animated images should have an AnimationState");
537 return mAnimationState->FirstFrameTimeout().AsEncodedValueDeprecated();
540 NS_IMETHODIMP_(already_AddRefed<SourceSurface>)
541 RasterImage::GetFrame(uint32_t aWhichFrame, uint32_t aFlags) {
542 return GetFrameAtSize(mSize.ToUnknownSize(), aWhichFrame, aFlags);
545 NS_IMETHODIMP_(already_AddRefed<SourceSurface>)
546 RasterImage::GetFrameAtSize(const IntSize& aSize, uint32_t aWhichFrame,
547 uint32_t aFlags) {
548 MOZ_ASSERT(aWhichFrame <= FRAME_MAX_VALUE);
550 AutoProfilerImagePaintMarker PROFILER_RAII(this);
551 #ifdef DEBUG
552 NotifyDrawingObservers();
553 #endif
555 if (aSize.IsEmpty() || aWhichFrame > FRAME_MAX_VALUE || mError) {
556 return nullptr;
559 auto size = OrientedIntSize::FromUnknownSize(aSize);
561 // Get the frame. If it's not there, it's probably the caller's fault for
562 // not waiting for the data to be loaded from the network or not passing
563 // FLAG_SYNC_DECODE.
564 LookupResult result = LookupFrame(size, aFlags, ToPlaybackType(aWhichFrame),
565 /* aMarkUsed = */ true);
566 if (!result) {
567 // The OS threw this frame away and we couldn't redecode it.
568 return nullptr;
571 return result.Surface()->GetSourceSurface();
574 NS_IMETHODIMP_(bool)
575 RasterImage::IsImageContainerAvailable(WindowRenderer* aRenderer,
576 uint32_t aFlags) {
577 return LoadHasSize();
580 NS_IMETHODIMP_(ImgDrawResult)
581 RasterImage::GetImageProvider(WindowRenderer* aRenderer,
582 const gfx::IntSize& aSize,
583 const Maybe<SVGImageContext>& aSVGContext,
584 const Maybe<ImageIntRegion>& aRegion,
585 uint32_t aFlags,
586 WebRenderImageProvider** aProvider) {
587 MOZ_ASSERT(NS_IsMainThread());
588 MOZ_ASSERT(aRenderer);
590 if (mError) {
591 return ImgDrawResult::BAD_IMAGE;
594 if (!LoadHasSize()) {
595 return ImgDrawResult::NOT_READY;
598 if (aSize.IsEmpty()) {
599 return ImgDrawResult::BAD_ARGS;
602 // We check the minimum size because while we support downscaling, we do not
603 // support upscaling. If aRequestedSize > mSize, we will never give a larger
604 // surface than mSize. If mSize > aRequestedSize, and mSize > maxTextureSize,
605 // we still want to use image containers if aRequestedSize <= maxTextureSize.
606 int32_t maxTextureSize = aRenderer->GetMaxTextureSize();
607 if (min(mSize.width, aSize.width) > maxTextureSize ||
608 min(mSize.height, aSize.height) > maxTextureSize) {
609 return ImgDrawResult::NOT_SUPPORTED;
612 AutoProfilerImagePaintMarker PROFILER_RAII(this);
613 #ifdef DEBUG
614 NotifyDrawingObservers();
615 #endif
617 // Get the frame. If it's not there, it's probably the caller's fault for
618 // not waiting for the data to be loaded from the network or not passing
619 // FLAG_SYNC_DECODE.
620 LookupResult result = LookupFrame(OrientedIntSize::FromUnknownSize(aSize),
621 aFlags, PlaybackType::eAnimated,
622 /* aMarkUsed = */ true);
623 if (!result) {
624 // The OS threw this frame away and we couldn't redecode it.
625 return ImgDrawResult::NOT_READY;
628 if (!result.Surface()->IsFinished()) {
629 result.Surface().TakeProvider(aProvider);
630 return ImgDrawResult::INCOMPLETE;
633 result.Surface().TakeProvider(aProvider);
634 switch (result.Type()) {
635 case MatchType::SUBSTITUTE_BECAUSE_NOT_FOUND:
636 case MatchType::SUBSTITUTE_BECAUSE_PENDING:
637 return ImgDrawResult::WRONG_SIZE;
638 default:
639 return ImgDrawResult::SUCCESS;
643 size_t RasterImage::SizeOfSourceWithComputedFallback(
644 SizeOfState& aState) const {
645 return mSourceBuffer->SizeOfIncludingThisWithComputedFallback(
646 aState.mMallocSizeOf);
649 bool RasterImage::SetMetadata(const ImageMetadata& aMetadata,
650 bool aFromMetadataDecode) {
651 MOZ_ASSERT(NS_IsMainThread());
653 if (mError) {
654 return true;
657 mResolution = aMetadata.GetResolution();
659 if (aMetadata.HasSize()) {
660 auto metadataSize = aMetadata.GetSize();
661 if (metadataSize.width < 0 || metadataSize.height < 0) {
662 NS_WARNING("Image has negative intrinsic size");
663 DoError();
664 return true;
667 MOZ_ASSERT(aMetadata.HasOrientation());
668 Orientation orientation = aMetadata.GetOrientation();
670 // If we already have a size, check the new size against the old one.
671 if (LoadHasSize() &&
672 (metadataSize != mSize || orientation != mOrientation)) {
673 NS_WARNING(
674 "Image changed size or orientation on redecode! "
675 "This should not happen!");
676 DoError();
677 return true;
680 // Set the size and flag that we have it.
681 mOrientation = orientation;
682 mSize = metadataSize;
683 mNativeSizes.Clear();
684 for (const auto& nativeSize : aMetadata.GetNativeSizes()) {
685 mNativeSizes.AppendElement(nativeSize);
687 StoreHasSize(true);
690 if (LoadHasSize() && aMetadata.HasAnimation() && !mAnimationState) {
691 // We're becoming animated, so initialize animation stuff.
692 mAnimationState.emplace(mAnimationMode);
693 mFrameAnimator = MakeUnique<FrameAnimator>(this, mSize.ToUnknownSize());
695 if (!StaticPrefs::image_mem_animated_discardable_AtStartup()) {
696 // We don't support discarding animated images (See bug 414259).
697 // Lock the image and throw away the key.
698 LockImage();
701 if (!aFromMetadataDecode) {
702 // The metadata decode reported that this image isn't animated, but we
703 // discovered that it actually was during the full decode. This is a
704 // rare failure that only occurs for corrupt images. To recover, we need
705 // to discard all existing surfaces and redecode.
706 return false;
710 if (mAnimationState) {
711 mAnimationState->SetLoopCount(aMetadata.GetLoopCount());
712 mAnimationState->SetFirstFrameTimeout(aMetadata.GetFirstFrameTimeout());
714 if (aMetadata.HasLoopLength()) {
715 mAnimationState->SetLoopLength(aMetadata.GetLoopLength());
717 if (aMetadata.HasFirstFrameRefreshArea()) {
718 mAnimationState->SetFirstFrameRefreshArea(
719 aMetadata.GetFirstFrameRefreshArea());
723 if (aMetadata.HasHotspot()) {
724 // NOTE(heycam): We shouldn't have any image formats that support both
725 // orientation and hotspots, so we assert that rather than add code
726 // to orient the hotspot point correctly.
727 MOZ_ASSERT(mOrientation.IsIdentity(), "Would need to orient hotspot point");
729 auto hotspot = aMetadata.GetHotspot();
730 mHotspot.x = std::max(std::min(hotspot.x, mSize.width - 1), 0);
731 mHotspot.y = std::max(std::min(hotspot.y, mSize.height - 1), 0);
734 return true;
737 NS_IMETHODIMP
738 RasterImage::SetAnimationMode(uint16_t aAnimationMode) {
739 if (mAnimationState) {
740 mAnimationState->SetAnimationMode(aAnimationMode);
742 return SetAnimationModeInternal(aAnimationMode);
745 //******************************************************************************
747 nsresult RasterImage::StartAnimation() {
748 if (mError) {
749 return NS_ERROR_FAILURE;
752 MOZ_ASSERT(ShouldAnimate(), "Should not animate!");
754 // If we're not ready to animate, then set mPendingAnimation, which will cause
755 // us to start animating if and when we do become ready.
756 StorePendingAnimation(!mAnimationState ||
757 mAnimationState->KnownFrameCount() < 1);
758 if (LoadPendingAnimation()) {
759 return NS_OK;
762 // Don't bother to animate if we're displaying the first frame forever.
763 if (mAnimationState->GetCurrentAnimationFrameIndex() == 0 &&
764 mAnimationState->FirstFrameTimeout() == FrameTimeout::Forever()) {
765 StoreAnimationFinished(true);
766 return NS_ERROR_ABORT;
769 // We need to set the time that this initial frame was first displayed, as
770 // this is used in AdvanceFrame().
771 mAnimationState->InitAnimationFrameTimeIfNecessary();
773 return NS_OK;
776 //******************************************************************************
777 nsresult RasterImage::StopAnimation() {
778 MOZ_ASSERT(mAnimating, "Should be animating!");
780 nsresult rv = NS_OK;
781 if (mError) {
782 rv = NS_ERROR_FAILURE;
783 } else {
784 mAnimationState->SetAnimationFrameTime(TimeStamp());
787 mAnimating = false;
788 return rv;
791 //******************************************************************************
792 NS_IMETHODIMP
793 RasterImage::ResetAnimation() {
794 if (mError) {
795 return NS_ERROR_FAILURE;
798 StorePendingAnimation(false);
800 if (mAnimationMode == kDontAnimMode || !mAnimationState ||
801 mAnimationState->GetCurrentAnimationFrameIndex() == 0) {
802 return NS_OK;
805 StoreAnimationFinished(false);
807 if (mAnimating) {
808 StopAnimation();
811 MOZ_ASSERT(mAnimationState, "Should have AnimationState");
812 MOZ_ASSERT(mFrameAnimator, "Should have FrameAnimator");
813 mFrameAnimator->ResetAnimation(*mAnimationState);
815 IntRect area = mAnimationState->FirstFrameRefreshArea();
816 NotifyProgress(NoProgress, OrientedIntRect::FromUnknownRect(area));
818 // Start the animation again. It may not have been running before, if
819 // mAnimationFinished was true before entering this function.
820 EvaluateAnimation();
822 return NS_OK;
825 //******************************************************************************
826 NS_IMETHODIMP_(void)
827 RasterImage::SetAnimationStartTime(const TimeStamp& aTime) {
828 if (mError || mAnimationMode == kDontAnimMode || mAnimating ||
829 !mAnimationState) {
830 return;
833 mAnimationState->SetAnimationFrameTime(aTime);
836 NS_IMETHODIMP_(float)
837 RasterImage::GetFrameIndex(uint32_t aWhichFrame) {
838 MOZ_ASSERT(aWhichFrame <= FRAME_MAX_VALUE, "Invalid argument");
839 return (aWhichFrame == FRAME_FIRST || !mAnimationState)
840 ? 0.0f
841 : mAnimationState->GetCurrentAnimationFrameIndex();
844 NS_IMETHODIMP_(IntRect)
845 RasterImage::GetImageSpaceInvalidationRect(const IntRect& aRect) {
846 // Note that we do not transform aRect into an UnorientedIntRect, since
847 // RasterImage::NotifyProgress notifies all consumers of the image using
848 // OrientedIntRect values. (This is unlike OrientedImage, which notifies
849 // using inner image coordinates.)
850 return aRect;
853 nsresult RasterImage::OnImageDataComplete(nsIRequest*, nsresult aStatus,
854 bool aLastPart) {
855 MOZ_ASSERT(NS_IsMainThread());
857 // Record that we have all the data we're going to get now.
858 StoreAllSourceData(true);
860 // Let decoders know that there won't be any more data coming.
861 mSourceBuffer->Complete(aStatus);
863 // Allow a synchronous metadata decode if mSyncLoad was set, or if we're
864 // running on a single thread (in which case waiting for the async metadata
865 // decoder could delay this image's load event quite a bit), or if this image
866 // is transient.
867 bool canSyncDecodeMetadata =
868 LoadSyncLoad() || LoadTransient() || DecodePool::NumberOfCores() < 2;
870 if (canSyncDecodeMetadata && !LoadHasSize()) {
871 // We're loading this image synchronously, so it needs to be usable after
872 // this call returns. Since we haven't gotten our size yet, we need to do a
873 // synchronous metadata decode here.
874 DecodeMetadata(FLAG_SYNC_DECODE);
877 // Determine our final status, giving precedence to Necko failure codes. We
878 // check after running the metadata decode in case it triggered an error.
879 nsresult finalStatus = mError ? NS_ERROR_FAILURE : NS_OK;
880 if (NS_FAILED(aStatus)) {
881 finalStatus = aStatus;
884 // If loading failed, report an error.
885 if (NS_FAILED(finalStatus)) {
886 DoError();
889 Progress loadProgress = LoadCompleteProgress(aLastPart, mError, finalStatus);
891 if (!LoadHasSize() && !mError) {
892 // We don't have our size yet, so we'll fire the load event in SetSize().
893 MOZ_ASSERT(!canSyncDecodeMetadata,
894 "Firing load async after metadata sync decode?");
895 mLoadProgress = Some(loadProgress);
896 return finalStatus;
899 NotifyForLoadEvent(loadProgress);
901 return finalStatus;
904 void RasterImage::NotifyForLoadEvent(Progress aProgress) {
905 MOZ_ASSERT(LoadHasSize() || mError,
906 "Need to know size before firing load event");
907 MOZ_ASSERT(
908 !LoadHasSize() || (mProgressTracker->GetProgress() & FLAG_SIZE_AVAILABLE),
909 "Should have notified that the size is available if we have it");
911 // If we encountered an error, make sure we notify for that as well.
912 if (mError) {
913 aProgress |= FLAG_HAS_ERROR;
916 // Notify our listeners, which will fire this image's load event.
917 NotifyProgress(aProgress);
920 nsresult RasterImage::OnImageDataAvailable(nsIRequest*,
921 nsIInputStream* aInputStream,
922 uint64_t, uint32_t aCount) {
923 nsresult rv = mSourceBuffer->AppendFromInputStream(aInputStream, aCount);
924 if (NS_SUCCEEDED(rv) && !LoadSomeSourceData()) {
925 StoreSomeSourceData(true);
926 if (!LoadSyncLoad()) {
927 // Create an async metadata decoder and verify we succeed in doing so.
928 rv = DecodeMetadata(DECODE_FLAGS_DEFAULT);
932 if (NS_FAILED(rv)) {
933 DoError();
935 return rv;
938 nsresult RasterImage::SetSourceSizeHint(uint32_t aSizeHint) {
939 if (aSizeHint == 0) {
940 return NS_OK;
943 nsresult rv = mSourceBuffer->ExpectLength(aSizeHint);
944 if (rv == NS_ERROR_OUT_OF_MEMORY) {
945 // Flush memory, try to get some back, and try again.
946 rv = nsMemory::HeapMinimize(true);
947 if (NS_SUCCEEDED(rv)) {
948 rv = mSourceBuffer->ExpectLength(aSizeHint);
952 return rv;
955 nsresult RasterImage::GetHotspotX(int32_t* aX) {
956 *aX = mHotspot.x;
957 return NS_OK;
960 nsresult RasterImage::GetHotspotY(int32_t* aY) {
961 *aY = mHotspot.y;
962 return NS_OK;
965 void RasterImage::Discard() {
966 MOZ_ASSERT(NS_IsMainThread());
967 MOZ_ASSERT(CanDiscard(), "Asked to discard but can't");
968 MOZ_ASSERT(!mAnimationState ||
969 StaticPrefs::image_mem_animated_discardable_AtStartup(),
970 "Asked to discard for animated image");
972 // Delete all the decoded frames.
973 SurfaceCache::RemoveImage(ImageKey(this));
975 if (mAnimationState) {
976 IntRect rect = mAnimationState->UpdateState(this, mSize.ToUnknownSize());
978 auto dirtyRect = OrientedIntRect::FromUnknownRect(rect);
979 NotifyProgress(NoProgress, dirtyRect);
982 // Notify that we discarded.
983 if (mProgressTracker) {
984 mProgressTracker->OnDiscard();
988 bool RasterImage::CanDiscard() {
989 return LoadAllSourceData() &&
990 // Can discard animated images if the pref is set
991 (!mAnimationState ||
992 StaticPrefs::image_mem_animated_discardable_AtStartup());
995 NS_IMETHODIMP
996 RasterImage::StartDecoding(uint32_t aFlags, uint32_t aWhichFrame) {
997 if (mError) {
998 return NS_ERROR_FAILURE;
1001 if (!LoadHasSize()) {
1002 StoreWantFullDecode(true);
1003 return NS_OK;
1006 uint32_t flags = (aFlags & FLAG_ASYNC_NOTIFY) | FLAG_SYNC_DECODE_IF_FAST |
1007 FLAG_HIGH_QUALITY_SCALING;
1008 return RequestDecodeForSize(mSize.ToUnknownSize(), flags, aWhichFrame);
1011 bool RasterImage::StartDecodingWithResult(uint32_t aFlags,
1012 uint32_t aWhichFrame) {
1013 if (mError) {
1014 return false;
1017 if (!LoadHasSize()) {
1018 StoreWantFullDecode(true);
1019 return false;
1022 uint32_t flags = (aFlags & FLAG_ASYNC_NOTIFY) | FLAG_SYNC_DECODE_IF_FAST |
1023 FLAG_HIGH_QUALITY_SCALING;
1024 LookupResult result = RequestDecodeForSizeInternal(mSize, flags, aWhichFrame);
1025 DrawableSurface surface = std::move(result.Surface());
1026 return surface && surface->IsFinished();
1029 imgIContainer::DecodeResult RasterImage::RequestDecodeWithResult(
1030 uint32_t aFlags, uint32_t aWhichFrame) {
1031 MOZ_ASSERT(NS_IsMainThread());
1033 if (mError) {
1034 return imgIContainer::DECODE_REQUEST_FAILED;
1037 uint32_t flags = aFlags | FLAG_ASYNC_NOTIFY;
1038 LookupResult result = RequestDecodeForSizeInternal(mSize, flags, aWhichFrame);
1039 DrawableSurface surface = std::move(result.Surface());
1040 if (surface && surface->IsFinished()) {
1041 return imgIContainer::DECODE_SURFACE_AVAILABLE;
1043 if (result.GetFailedToRequestDecode()) {
1044 return imgIContainer::DECODE_REQUEST_FAILED;
1046 return imgIContainer::DECODE_REQUESTED;
1049 NS_IMETHODIMP
1050 RasterImage::RequestDecodeForSize(const IntSize& aSize, uint32_t aFlags,
1051 uint32_t aWhichFrame) {
1052 MOZ_ASSERT(NS_IsMainThread());
1054 if (mError) {
1055 return NS_ERROR_FAILURE;
1058 RequestDecodeForSizeInternal(OrientedIntSize::FromUnknownSize(aSize), aFlags,
1059 aWhichFrame);
1061 return NS_OK;
1064 LookupResult RasterImage::RequestDecodeForSizeInternal(
1065 const OrientedIntSize& aSize, uint32_t aFlags, uint32_t aWhichFrame) {
1066 MOZ_ASSERT(NS_IsMainThread());
1068 if (aWhichFrame > FRAME_MAX_VALUE) {
1069 return LookupResult(MatchType::NOT_FOUND);
1072 if (mError) {
1073 LookupResult result = LookupResult(MatchType::NOT_FOUND);
1074 result.SetFailedToRequestDecode();
1075 return result;
1078 if (!LoadHasSize()) {
1079 StoreWantFullDecode(true);
1080 return LookupResult(MatchType::NOT_FOUND);
1083 // Decide whether to sync decode images we can decode quickly. Here we are
1084 // explicitly trading off flashing for responsiveness in the case that we're
1085 // redecoding an image (see bug 845147).
1086 bool shouldSyncDecodeIfFast =
1087 !LoadHasBeenDecoded() && (aFlags & FLAG_SYNC_DECODE_IF_FAST);
1089 uint32_t flags =
1090 shouldSyncDecodeIfFast ? aFlags : aFlags & ~FLAG_SYNC_DECODE_IF_FAST;
1092 // Perform a frame lookup, which will implicitly start decoding if needed.
1093 return LookupFrame(aSize, flags, ToPlaybackType(aWhichFrame),
1094 /* aMarkUsed = */ false);
1097 static bool LaunchDecodingTask(IDecodingTask* aTask, RasterImage* aImage,
1098 uint32_t aFlags, bool aHaveSourceData) {
1099 if (aHaveSourceData) {
1100 nsCString uri(aImage->GetURIString());
1102 // If we have all the data, we can sync decode if requested.
1103 if (aFlags & imgIContainer::FLAG_SYNC_DECODE) {
1104 DecodePool::Singleton()->SyncRunIfPossible(aTask, uri);
1105 return true;
1108 if (aFlags & imgIContainer::FLAG_SYNC_DECODE_IF_FAST) {
1109 return DecodePool::Singleton()->SyncRunIfPreferred(aTask, uri);
1113 // Perform an async decode. We also take this path if we don't have all the
1114 // source data yet, since sync decoding is impossible in that situation.
1115 DecodePool::Singleton()->AsyncRun(aTask);
1116 return false;
1119 void RasterImage::Decode(const OrientedIntSize& aSize, uint32_t aFlags,
1120 PlaybackType aPlaybackType, bool& aOutRanSync,
1121 bool& aOutFailed) {
1122 MOZ_ASSERT(NS_IsMainThread());
1124 if (mError) {
1125 aOutFailed = true;
1126 return;
1129 // If we don't have a size yet, we can't do any other decoding.
1130 if (!LoadHasSize()) {
1131 StoreWantFullDecode(true);
1132 return;
1135 // We're about to decode again, which may mean that some of the previous sizes
1136 // we've decoded at aren't useful anymore. We can allow them to expire from
1137 // the cache by unlocking them here. When the decode finishes, it will send an
1138 // invalidation that will cause all instances of this image to redraw. If this
1139 // image is locked, any surfaces that are still useful will become locked
1140 // again when LookupFrame touches them, and the remainder will eventually
1141 // expire.
1142 SurfaceCache::UnlockEntries(ImageKey(this));
1144 // Determine which flags we need to decode this image with.
1145 DecoderFlags decoderFlags = DefaultDecoderFlags();
1146 if (aFlags & FLAG_ASYNC_NOTIFY) {
1147 decoderFlags |= DecoderFlags::ASYNC_NOTIFY;
1149 if (LoadTransient()) {
1150 decoderFlags |= DecoderFlags::IMAGE_IS_TRANSIENT;
1152 if (LoadHasBeenDecoded()) {
1153 decoderFlags |= DecoderFlags::IS_REDECODE;
1155 if ((aFlags & FLAG_SYNC_DECODE) || !(aFlags & FLAG_HIGH_QUALITY_SCALING)) {
1156 // Used SurfaceCache::Lookup instead of SurfaceCache::LookupBestMatch. That
1157 // means the caller can handle a differently sized surface to be returned
1158 // at any point.
1159 decoderFlags |= DecoderFlags::CANNOT_SUBSTITUTE;
1162 SurfaceFlags surfaceFlags = ToSurfaceFlags(aFlags);
1163 if (IsOpaque()) {
1164 // If there's no transparency, it doesn't matter whether we premultiply
1165 // alpha or not.
1166 surfaceFlags &= ~SurfaceFlags::NO_PREMULTIPLY_ALPHA;
1169 // Create a decoder.
1170 RefPtr<IDecodingTask> task;
1171 nsresult rv;
1172 bool animated = mAnimationState && aPlaybackType == PlaybackType::eAnimated;
1173 if (animated) {
1174 size_t currentFrame = mAnimationState->GetCurrentAnimationFrameIndex();
1175 rv = DecoderFactory::CreateAnimationDecoder(
1176 mDecoderType, WrapNotNull(this), mSourceBuffer, mSize.ToUnknownSize(),
1177 decoderFlags, surfaceFlags, currentFrame, getter_AddRefs(task));
1178 } else {
1179 rv = DecoderFactory::CreateDecoder(mDecoderType, WrapNotNull(this),
1180 mSourceBuffer, mSize.ToUnknownSize(),
1181 aSize.ToUnknownSize(), decoderFlags,
1182 surfaceFlags, getter_AddRefs(task));
1185 if (rv == NS_ERROR_ALREADY_INITIALIZED) {
1186 // We raced with an already pending decoder, and it finished before we
1187 // managed to insert the new decoder. Pretend we did a sync call to make
1188 // the caller lookup in the surface cache again.
1189 MOZ_ASSERT(!task);
1190 aOutRanSync = true;
1191 return;
1194 if (animated) {
1195 // We pass false for aAllowInvalidation because we may be asked to use
1196 // async notifications. Any potential invalidation here will be sent when
1197 // RequestRefresh is called, or NotifyDecodeComplete.
1198 #ifdef DEBUG
1199 IntRect rect =
1200 #endif
1201 mAnimationState->UpdateState(this, mSize.ToUnknownSize(), false);
1202 MOZ_ASSERT(rect.IsEmpty());
1205 // Make sure DecoderFactory was able to create a decoder successfully.
1206 if (NS_FAILED(rv)) {
1207 MOZ_ASSERT(!task);
1208 aOutFailed = true;
1209 return;
1212 MOZ_ASSERT(task);
1213 mDecodeCount++;
1215 // We're ready to decode; start the decoder.
1216 aOutRanSync = LaunchDecodingTask(task, this, aFlags, LoadAllSourceData());
1219 NS_IMETHODIMP
1220 RasterImage::DecodeMetadata(uint32_t aFlags) {
1221 if (mError) {
1222 return NS_ERROR_FAILURE;
1225 MOZ_ASSERT(!LoadHasSize(), "Should not do unnecessary metadata decodes");
1227 // Create a decoder.
1228 RefPtr<IDecodingTask> task = DecoderFactory::CreateMetadataDecoder(
1229 mDecoderType, WrapNotNull(this), mSourceBuffer);
1231 // Make sure DecoderFactory was able to create a decoder successfully.
1232 if (!task) {
1233 return NS_ERROR_FAILURE;
1236 // We're ready to decode; start the decoder.
1237 LaunchDecodingTask(task, this, aFlags, LoadAllSourceData());
1238 return NS_OK;
1241 void RasterImage::RecoverFromInvalidFrames(const OrientedIntSize& aSize,
1242 uint32_t aFlags) {
1243 if (!LoadHasSize()) {
1244 return;
1247 NS_WARNING("A RasterImage's frames became invalid. Attempting to recover...");
1249 // Discard all existing frames, since they're probably all now invalid.
1250 SurfaceCache::RemoveImage(ImageKey(this));
1252 // Relock the image if it's supposed to be locked.
1253 if (mLockCount > 0) {
1254 SurfaceCache::LockImage(ImageKey(this));
1257 bool unused1, unused2;
1259 // Animated images require some special handling, because we normally require
1260 // that they never be discarded.
1261 if (mAnimationState) {
1262 Decode(mSize, aFlags | FLAG_SYNC_DECODE, PlaybackType::eAnimated, unused1,
1263 unused2);
1264 ResetAnimation();
1265 return;
1268 // For non-animated images, it's fine to recover using an async decode.
1269 Decode(aSize, aFlags, PlaybackType::eStatic, unused1, unused2);
1272 bool RasterImage::CanDownscaleDuringDecode(const OrientedIntSize& aSize,
1273 uint32_t aFlags) {
1274 // Check basic requirements: downscale-during-decode is enabled, Skia is
1275 // available, this image isn't transient, we have all the source data and know
1276 // our size, and the flags allow us to do it.
1277 if (!LoadHasSize() || LoadTransient() ||
1278 !StaticPrefs::image_downscale_during_decode_enabled() ||
1279 !(aFlags & imgIContainer::FLAG_HIGH_QUALITY_SCALING)) {
1280 return false;
1283 // We don't downscale animated images during decode.
1284 if (mAnimationState) {
1285 return false;
1288 // Never upscale.
1289 if (aSize.width >= mSize.width || aSize.height >= mSize.height) {
1290 return false;
1293 // Zero or negative width or height is unacceptable.
1294 if (aSize.width < 1 || aSize.height < 1) {
1295 return false;
1298 // There's no point in scaling if we can't store the result.
1299 if (!SurfaceCache::CanHold(aSize.ToUnknownSize())) {
1300 return false;
1303 return true;
1306 ImgDrawResult RasterImage::DrawInternal(DrawableSurface&& aSurface,
1307 gfxContext* aContext,
1308 const OrientedIntSize& aSize,
1309 const ImageRegion& aRegion,
1310 SamplingFilter aSamplingFilter,
1311 uint32_t aFlags, float aOpacity) {
1312 gfxContextMatrixAutoSaveRestore saveMatrix(aContext);
1313 ImageRegion region(aRegion);
1314 bool frameIsFinished = aSurface->IsFinished();
1316 AutoProfilerImagePaintMarker PROFILER_RAII(this);
1317 #ifdef DEBUG
1318 NotifyDrawingObservers();
1319 #endif
1321 // By now we may have a frame with the requested size. If not, we need to
1322 // adjust the drawing parameters accordingly.
1323 IntSize finalSize = aSurface->GetSize();
1324 bool couldRedecodeForBetterFrame = false;
1325 if (finalSize != aSize.ToUnknownSize()) {
1326 gfx::Size scale(double(aSize.width) / finalSize.width,
1327 double(aSize.height) / finalSize.height);
1328 aContext->Multiply(gfxMatrix::Scaling(scale.width, scale.height));
1329 region.Scale(1.0 / scale.width, 1.0 / scale.height);
1331 couldRedecodeForBetterFrame = CanDownscaleDuringDecode(aSize, aFlags);
1334 if (!aSurface->Draw(aContext, region, aSamplingFilter, aFlags, aOpacity)) {
1335 RecoverFromInvalidFrames(aSize, aFlags);
1336 return ImgDrawResult::TEMPORARY_ERROR;
1338 if (!frameIsFinished) {
1339 return ImgDrawResult::INCOMPLETE;
1341 if (couldRedecodeForBetterFrame) {
1342 return ImgDrawResult::WRONG_SIZE;
1344 return ImgDrawResult::SUCCESS;
1347 //******************************************************************************
1348 NS_IMETHODIMP_(ImgDrawResult)
1349 RasterImage::Draw(gfxContext* aContext, const IntSize& aSize,
1350 const ImageRegion& aRegion, uint32_t aWhichFrame,
1351 SamplingFilter aSamplingFilter,
1352 const Maybe<SVGImageContext>& /*aSVGContext - ignored*/,
1353 uint32_t aFlags, float aOpacity) {
1354 if (aWhichFrame > FRAME_MAX_VALUE) {
1355 return ImgDrawResult::BAD_ARGS;
1358 if (mError) {
1359 return ImgDrawResult::BAD_IMAGE;
1362 // Illegal -- you can't draw with non-default decode flags.
1363 // (Disabling colorspace conversion might make sense to allow, but
1364 // we don't currently.)
1365 if (ToSurfaceFlags(aFlags) != DefaultSurfaceFlags()) {
1366 return ImgDrawResult::BAD_ARGS;
1369 if (!aContext) {
1370 return ImgDrawResult::BAD_ARGS;
1373 if (mAnimationConsumers == 0 && mAnimationState) {
1374 SendOnUnlockedDraw(aFlags);
1377 // If we're not using SamplingFilter::GOOD, we shouldn't high-quality scale or
1378 // downscale during decode.
1379 uint32_t flags = aSamplingFilter == SamplingFilter::GOOD
1380 ? aFlags
1381 : aFlags & ~FLAG_HIGH_QUALITY_SCALING;
1383 auto size = OrientedIntSize::FromUnknownSize(aSize);
1384 LookupResult result = LookupFrame(size, flags, ToPlaybackType(aWhichFrame),
1385 /* aMarkUsed = */ true);
1386 if (!result) {
1387 // Getting the frame (above) touches the image and kicks off decoding.
1388 if (mDrawStartTime.IsNull()) {
1389 mDrawStartTime = TimeStamp::Now();
1391 return ImgDrawResult::NOT_READY;
1394 bool shouldRecordTelemetry =
1395 !mDrawStartTime.IsNull() && result.Surface()->IsFinished();
1397 ImgDrawResult drawResult =
1398 DrawInternal(std::move(result.Surface()), aContext, size, aRegion,
1399 aSamplingFilter, flags, aOpacity);
1401 if (shouldRecordTelemetry) {
1402 TimeDuration drawLatency = TimeStamp::Now() - mDrawStartTime;
1403 Telemetry::Accumulate(Telemetry::IMAGE_DECODE_ON_DRAW_LATENCY,
1404 int32_t(drawLatency.ToMicroseconds()));
1405 mDrawStartTime = TimeStamp();
1408 return drawResult;
1411 //******************************************************************************
1413 NS_IMETHODIMP
1414 RasterImage::LockImage() {
1415 MOZ_ASSERT(NS_IsMainThread(),
1416 "Main thread to encourage serialization with UnlockImage");
1417 if (mError) {
1418 return NS_ERROR_FAILURE;
1421 // Increment the lock count
1422 mLockCount++;
1424 // Lock this image's surfaces in the SurfaceCache.
1425 if (mLockCount == 1) {
1426 SurfaceCache::LockImage(ImageKey(this));
1429 return NS_OK;
1432 //******************************************************************************
1434 NS_IMETHODIMP
1435 RasterImage::UnlockImage() {
1436 MOZ_ASSERT(NS_IsMainThread(),
1437 "Main thread to encourage serialization with LockImage");
1438 if (mError) {
1439 return NS_ERROR_FAILURE;
1442 // It's an error to call this function if the lock count is 0
1443 MOZ_ASSERT(mLockCount > 0, "Calling UnlockImage with mLockCount == 0!");
1444 if (mLockCount == 0) {
1445 return NS_ERROR_ABORT;
1448 // Decrement our lock count
1449 mLockCount--;
1451 // Unlock this image's surfaces in the SurfaceCache.
1452 if (mLockCount == 0) {
1453 SurfaceCache::UnlockImage(ImageKey(this));
1456 return NS_OK;
1459 //******************************************************************************
1461 NS_IMETHODIMP
1462 RasterImage::RequestDiscard() {
1463 if (LoadDiscardable() && // Enabled at creation time...
1464 mLockCount == 0 && // ...not temporarily disabled...
1465 CanDiscard()) {
1466 Discard();
1469 return NS_OK;
1472 // Idempotent error flagging routine. If a decoder is open, shuts it down.
1473 void RasterImage::DoError() {
1474 // If we've flagged an error before, we have nothing to do
1475 if (mError) {
1476 return;
1479 // We can't safely handle errors off-main-thread, so dispatch a worker to
1480 // do it.
1481 if (!NS_IsMainThread()) {
1482 HandleErrorWorker::DispatchIfNeeded(this);
1483 return;
1486 // Put the container in an error state.
1487 mError = true;
1489 // Stop animation and release our FrameAnimator.
1490 if (mAnimating) {
1491 StopAnimation();
1493 mAnimationState = Nothing();
1494 mFrameAnimator = nullptr;
1496 // Release all locks.
1497 mLockCount = 0;
1498 SurfaceCache::UnlockImage(ImageKey(this));
1500 // Release all frames from the surface cache.
1501 SurfaceCache::RemoveImage(ImageKey(this));
1503 // Invalidate to get rid of any partially-drawn image content.
1504 auto dirtyRect = OrientedIntRect({0, 0}, mSize);
1505 NotifyProgress(NoProgress, dirtyRect);
1507 MOZ_LOG(gImgLog, LogLevel::Error,
1508 ("RasterImage: [this=%p] Error detected for image\n", this));
1511 /* static */
1512 void RasterImage::HandleErrorWorker::DispatchIfNeeded(RasterImage* aImage) {
1513 RefPtr<HandleErrorWorker> worker = new HandleErrorWorker(aImage);
1514 NS_DispatchToMainThread(worker);
1517 RasterImage::HandleErrorWorker::HandleErrorWorker(RasterImage* aImage)
1518 : Runnable("image::RasterImage::HandleErrorWorker"), mImage(aImage) {
1519 MOZ_ASSERT(mImage, "Should have image");
1522 NS_IMETHODIMP
1523 RasterImage::HandleErrorWorker::Run() {
1524 mImage->DoError();
1526 return NS_OK;
1529 bool RasterImage::ShouldAnimate() {
1530 return ImageResource::ShouldAnimate() && mAnimationState &&
1531 mAnimationState->KnownFrameCount() >= 1 && !LoadAnimationFinished();
1534 #ifdef DEBUG
1535 NS_IMETHODIMP
1536 RasterImage::GetFramesNotified(uint32_t* aFramesNotified) {
1537 NS_ENSURE_ARG_POINTER(aFramesNotified);
1539 *aFramesNotified = mFramesNotified;
1541 return NS_OK;
1543 #endif
1545 void RasterImage::NotifyProgress(
1546 Progress aProgress,
1547 const OrientedIntRect& aInvalidRect /* = OrientedIntRect() */,
1548 const Maybe<uint32_t>& aFrameCount /* = Nothing() */,
1549 DecoderFlags aDecoderFlags /* = DefaultDecoderFlags() */,
1550 SurfaceFlags aSurfaceFlags /* = DefaultSurfaceFlags() */) {
1551 MOZ_ASSERT(NS_IsMainThread());
1553 // Ensure that we stay alive long enough to finish notifying.
1554 RefPtr<RasterImage> image = this;
1556 OrientedIntRect invalidRect = aInvalidRect;
1558 if (!(aDecoderFlags & DecoderFlags::FIRST_FRAME_ONLY)) {
1559 // We may have decoded new animation frames; update our animation state.
1560 MOZ_ASSERT_IF(aFrameCount && *aFrameCount > 1, mAnimationState || mError);
1561 if (mAnimationState && aFrameCount) {
1562 mAnimationState->UpdateKnownFrameCount(*aFrameCount);
1565 // If we should start animating right now, do so.
1566 if (mAnimationState && aFrameCount == Some(1u) && LoadPendingAnimation() &&
1567 ShouldAnimate()) {
1568 StartAnimation();
1571 if (mAnimationState) {
1572 IntRect rect = mAnimationState->UpdateState(this, mSize.ToUnknownSize());
1574 invalidRect.UnionRect(invalidRect,
1575 OrientedIntRect::FromUnknownRect(rect));
1579 // Tell the observers what happened.
1580 image->mProgressTracker->SyncNotifyProgress(aProgress,
1581 invalidRect.ToUnknownRect());
1584 void RasterImage::NotifyDecodeComplete(
1585 const DecoderFinalStatus& aStatus, const ImageMetadata& aMetadata,
1586 const DecoderTelemetry& aTelemetry, Progress aProgress,
1587 const OrientedIntRect& aInvalidRect, const Maybe<uint32_t>& aFrameCount,
1588 DecoderFlags aDecoderFlags, SurfaceFlags aSurfaceFlags) {
1589 MOZ_ASSERT(NS_IsMainThread());
1591 // If the decoder detected an error, log it to the error console.
1592 if (aStatus.mShouldReportError) {
1593 ReportDecoderError();
1596 // Record all the metadata the decoder gathered about this image.
1597 bool metadataOK = SetMetadata(aMetadata, aStatus.mWasMetadataDecode);
1598 if (!metadataOK) {
1599 // This indicates a serious error that requires us to discard all existing
1600 // surfaces and redecode to recover. We'll drop the results from this
1601 // decoder on the floor, since they aren't valid.
1602 RecoverFromInvalidFrames(mSize, FromSurfaceFlags(aSurfaceFlags));
1603 return;
1606 MOZ_ASSERT(mError || LoadHasSize() || !aMetadata.HasSize(),
1607 "SetMetadata should've gotten a size");
1609 if (!aStatus.mWasMetadataDecode && aStatus.mFinished) {
1610 // Flag that we've been decoded before.
1611 StoreHasBeenDecoded(true);
1614 // Send out any final notifications.
1615 NotifyProgress(aProgress, aInvalidRect, aFrameCount, aDecoderFlags,
1616 aSurfaceFlags);
1618 if (!(aDecoderFlags & DecoderFlags::FIRST_FRAME_ONLY)) {
1619 // We may have decoded new animation frames; update our animation state.
1620 MOZ_ASSERT_IF(aFrameCount && *aFrameCount > 1, mAnimationState || mError);
1621 if (mAnimationState && aFrameCount) {
1622 mAnimationState->UpdateKnownFrameCount(*aFrameCount);
1625 // If we should start animating right now, do so.
1626 if (mAnimationState && aFrameCount == Some(1u) && LoadPendingAnimation() &&
1627 ShouldAnimate()) {
1628 StartAnimation();
1631 if (mAnimationState && LoadHasBeenDecoded()) {
1632 // We've finished a full decode of all animation frames and our
1633 // AnimationState has been notified about them all, so let it know not to
1634 // expect anymore.
1635 mAnimationState->NotifyDecodeComplete();
1637 IntRect rect = mAnimationState->UpdateState(this, mSize.ToUnknownSize());
1639 if (!rect.IsEmpty()) {
1640 auto dirtyRect = OrientedIntRect::FromUnknownRect(rect);
1641 NotifyProgress(NoProgress, dirtyRect);
1646 // Do some telemetry if this isn't a metadata decode.
1647 if (!aStatus.mWasMetadataDecode) {
1648 if (aTelemetry.mChunkCount) {
1649 Telemetry::Accumulate(Telemetry::IMAGE_DECODE_CHUNKS,
1650 aTelemetry.mChunkCount);
1653 if (aStatus.mFinished) {
1654 Telemetry::Accumulate(Telemetry::IMAGE_DECODE_TIME,
1655 int32_t(aTelemetry.mDecodeTime.ToMicroseconds()));
1657 if (aTelemetry.mSpeedHistogram && aTelemetry.mBytesDecoded) {
1658 Telemetry::Accumulate(*aTelemetry.mSpeedHistogram, aTelemetry.Speed());
1663 // Only act on errors if we have no usable frames from the decoder.
1664 if (aStatus.mHadError &&
1665 (!mAnimationState || mAnimationState->KnownFrameCount() == 0)) {
1666 DoError();
1667 } else if (aStatus.mWasMetadataDecode && !LoadHasSize()) {
1668 DoError();
1671 // XXX(aosmond): Can we get this far without mFinished == true?
1672 if (aStatus.mFinished && aStatus.mWasMetadataDecode) {
1673 // If we were waiting to fire the load event, go ahead and fire it now.
1674 if (mLoadProgress) {
1675 NotifyForLoadEvent(*mLoadProgress);
1676 mLoadProgress = Nothing();
1679 // If we were a metadata decode and a full decode was requested, do it.
1680 if (LoadWantFullDecode()) {
1681 StoreWantFullDecode(false);
1682 RequestDecodeForSizeInternal(
1683 mSize, DECODE_FLAGS_DEFAULT | FLAG_HIGH_QUALITY_SCALING,
1684 FRAME_CURRENT);
1689 void RasterImage::ReportDecoderError() {
1690 nsCOMPtr<nsIConsoleService> consoleService =
1691 do_GetService(NS_CONSOLESERVICE_CONTRACTID);
1692 nsCOMPtr<nsIScriptError> errorObject =
1693 do_CreateInstance(NS_SCRIPTERROR_CONTRACTID);
1695 if (consoleService && errorObject) {
1696 nsAutoString msg(u"Image corrupt or truncated."_ns);
1697 nsAutoString src;
1698 if (GetURI()) {
1699 nsAutoCString uri;
1700 if (!GetSpecTruncatedTo1k(uri)) {
1701 msg += u" URI in this note truncated due to length."_ns;
1703 CopyUTF8toUTF16(uri, src);
1705 if (NS_SUCCEEDED(errorObject->InitWithWindowID(msg, src, u""_ns, 0, 0,
1706 nsIScriptError::errorFlag,
1707 "Image", InnerWindowID()))) {
1708 consoleService->LogMessage(errorObject);
1713 already_AddRefed<imgIContainer> RasterImage::Unwrap() {
1714 nsCOMPtr<imgIContainer> self(this);
1715 return self.forget();
1718 void RasterImage::PropagateUseCounters(dom::Document*) {
1719 // No use counters.
1722 IntSize RasterImage::OptimalImageSizeForDest(const gfxSize& aDest,
1723 uint32_t aWhichFrame,
1724 SamplingFilter aSamplingFilter,
1725 uint32_t aFlags) {
1726 MOZ_ASSERT(aDest.width >= 0 || ceil(aDest.width) <= INT32_MAX ||
1727 aDest.height >= 0 || ceil(aDest.height) <= INT32_MAX,
1728 "Unexpected destination size");
1730 if (mSize.IsEmpty() || aDest.IsEmpty()) {
1731 return IntSize(0, 0);
1734 auto dest = OrientedIntSize::FromUnknownSize(
1735 IntSize::Ceil(aDest.width, aDest.height));
1737 if (aSamplingFilter == SamplingFilter::GOOD &&
1738 CanDownscaleDuringDecode(dest, aFlags)) {
1739 return dest.ToUnknownSize();
1742 // We can't scale to this size. Use our intrinsic size for now.
1743 return mSize.ToUnknownSize();
1746 } // namespace image
1747 } // namespace mozilla