Bug 1890277: part 4) Add CSPParser support for the `trusted-types` directive, guarded...
[gecko.git] / image / RasterImage.cpp
blob06e44bda018b121516ed70aab5825998ee7f4d75
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 "LookupResult.h"
23 #include "OrientedImage.h"
24 #include "SourceBuffer.h"
25 #include "SurfaceCache.h"
26 #include "gfx2DGlue.h"
27 #include "gfxContext.h"
28 #include "gfxPlatform.h"
29 #include "mozilla/ClearOnShutdown.h"
30 #include "mozilla/DebugOnly.h"
31 #include "mozilla/Likely.h"
32 #include "mozilla/MemoryReporting.h"
33 #include "mozilla/RefPtr.h"
34 #include "mozilla/SizeOfState.h"
35 #include "mozilla/StaticPrefs_image.h"
36 #include "mozilla/Telemetry.h"
37 #include "mozilla/TimeStamp.h"
39 #include "mozilla/gfx/2D.h"
40 #include "mozilla/gfx/Scale.h"
41 #include "nsComponentManagerUtils.h"
42 #include "nsError.h"
43 #include "nsIConsoleService.h"
44 #include "nsIInputStream.h"
45 #include "nsIScriptError.h"
46 #include "nsISupportsPrimitives.h"
47 #include "nsMemory.h"
48 #include "nsPresContext.h"
49 #include "nsProperties.h"
50 #include "prenv.h"
51 #include "prsystem.h"
52 #include "WindowRenderer.h"
54 namespace mozilla {
56 using namespace gfx;
57 using namespace layers;
59 namespace image {
61 using std::ceil;
62 using std::min;
64 #ifndef DEBUG
65 NS_IMPL_ISUPPORTS(RasterImage, imgIContainer)
66 #else
67 NS_IMPL_ISUPPORTS(RasterImage, imgIContainer, imgIContainerDebug)
68 #endif
70 //******************************************************************************
71 RasterImage::RasterImage(nsIURI* aURI /* = nullptr */)
72 : ImageResource(aURI), // invoke superclass's constructor
73 mSize(0, 0),
74 mLockCount(0),
75 mDecoderType(DecoderType::UNKNOWN),
76 mDecodeCount(0),
77 #ifdef DEBUG
78 mFramesNotified(0),
79 #endif
80 mSourceBuffer(MakeNotNull<SourceBuffer*>()) {
83 //******************************************************************************
84 RasterImage::~RasterImage() {
85 // Make sure our SourceBuffer is marked as complete. This will ensure that any
86 // outstanding decoders terminate.
87 if (!mSourceBuffer->IsComplete()) {
88 mSourceBuffer->Complete(NS_ERROR_ABORT);
91 // Release all frames from the surface cache.
92 SurfaceCache::RemoveImage(ImageKey(this));
94 // Record Telemetry.
95 Telemetry::Accumulate(Telemetry::IMAGE_DECODE_COUNT, mDecodeCount);
98 nsresult RasterImage::Init(const char* aMimeType, uint32_t aFlags) {
99 // We don't support re-initialization
100 if (mInitialized) {
101 return NS_ERROR_ILLEGAL_VALUE;
104 // Not sure an error can happen before init, but be safe
105 if (mError) {
106 return NS_ERROR_FAILURE;
109 // We want to avoid redecodes for transient images.
110 MOZ_ASSERT_IF(aFlags & INIT_FLAG_TRANSIENT,
111 !(aFlags & INIT_FLAG_DISCARDABLE));
113 // Store initialization data
114 StoreDiscardable(!!(aFlags & INIT_FLAG_DISCARDABLE));
115 StoreWantFullDecode(!!(aFlags & INIT_FLAG_DECODE_IMMEDIATELY));
116 StoreTransient(!!(aFlags & INIT_FLAG_TRANSIENT));
117 StoreSyncLoad(!!(aFlags & INIT_FLAG_SYNC_LOAD));
119 // Use the MIME type to select a decoder type, and make sure there *is* a
120 // decoder for this MIME type.
121 NS_ENSURE_ARG_POINTER(aMimeType);
122 mDecoderType = DecoderFactory::GetDecoderType(aMimeType);
123 if (mDecoderType == DecoderType::UNKNOWN) {
124 return NS_ERROR_FAILURE;
127 // Lock this image's surfaces in the SurfaceCache if we're not discardable.
128 if (!LoadDiscardable()) {
129 mLockCount++;
130 SurfaceCache::LockImage(ImageKey(this));
133 // Set the default flags according to the decoder type to allow preferences to
134 // be stored if necessary.
135 mDefaultDecoderFlags =
136 DecoderFactory::GetDefaultDecoderFlagsForType(mDecoderType);
138 // Mark us as initialized
139 mInitialized = true;
141 return NS_OK;
144 //******************************************************************************
145 NS_IMETHODIMP_(void)
146 RasterImage::RequestRefresh(const TimeStamp& aTime) {
147 if (HadRecentRefresh(aTime)) {
148 return;
151 EvaluateAnimation();
153 if (!mAnimating) {
154 return;
157 RefreshResult res;
158 if (mAnimationState) {
159 MOZ_ASSERT(mFrameAnimator);
160 res = mFrameAnimator->RequestRefresh(*mAnimationState, aTime);
163 #ifdef DEBUG
164 if (res.mFrameAdvanced) {
165 mFramesNotified++;
167 #endif
169 // Notify listeners that our frame has actually changed, but do this only
170 // once for all frames that we've now passed (if AdvanceFrame() was called
171 // more than once).
172 if (!res.mDirtyRect.IsEmpty() || res.mFrameAdvanced) {
173 auto dirtyRect = OrientedIntRect::FromUnknownRect(res.mDirtyRect);
174 NotifyProgress(NoProgress, dirtyRect);
177 if (res.mAnimationFinished) {
178 StoreAnimationFinished(true);
179 EvaluateAnimation();
183 //******************************************************************************
184 NS_IMETHODIMP
185 RasterImage::GetWidth(int32_t* aWidth) {
186 NS_ENSURE_ARG_POINTER(aWidth);
188 if (mError) {
189 *aWidth = 0;
190 return NS_ERROR_FAILURE;
193 *aWidth = mSize.width;
194 return NS_OK;
197 //******************************************************************************
198 NS_IMETHODIMP
199 RasterImage::GetHeight(int32_t* aHeight) {
200 NS_ENSURE_ARG_POINTER(aHeight);
202 if (mError) {
203 *aHeight = 0;
204 return NS_ERROR_FAILURE;
207 *aHeight = mSize.height;
208 return NS_OK;
211 //******************************************************************************
212 void RasterImage::MediaFeatureValuesChangedAllDocuments(
213 const mozilla::MediaFeatureChange& aChange) {}
215 //******************************************************************************
216 nsresult RasterImage::GetNativeSizes(nsTArray<IntSize>& aNativeSizes) {
217 if (mError) {
218 return NS_ERROR_FAILURE;
221 aNativeSizes.Clear();
223 if (mNativeSizes.IsEmpty()) {
224 aNativeSizes.AppendElement(mSize.ToUnknownSize());
225 } else {
226 for (const auto& size : mNativeSizes) {
227 aNativeSizes.AppendElement(size.ToUnknownSize());
231 return NS_OK;
234 //******************************************************************************
235 size_t RasterImage::GetNativeSizesLength() {
236 if (mError || !LoadHasSize()) {
237 return 0;
240 if (mNativeSizes.IsEmpty()) {
241 return 1;
244 return mNativeSizes.Length();
247 //******************************************************************************
248 NS_IMETHODIMP
249 RasterImage::GetIntrinsicSize(nsSize* aSize) {
250 if (mError) {
251 return NS_ERROR_FAILURE;
254 *aSize = nsSize(nsPresContext::CSSPixelsToAppUnits(mSize.width),
255 nsPresContext::CSSPixelsToAppUnits(mSize.height));
256 return NS_OK;
259 //******************************************************************************
260 Maybe<AspectRatio> RasterImage::GetIntrinsicRatio() {
261 if (mError) {
262 return Nothing();
265 return Some(AspectRatio::FromSize(mSize.width, mSize.height));
268 NS_IMETHODIMP_(Orientation)
269 RasterImage::GetOrientation() { return mOrientation; }
271 NS_IMETHODIMP_(Resolution)
272 RasterImage::GetResolution() { return mResolution; }
274 //******************************************************************************
275 NS_IMETHODIMP
276 RasterImage::GetType(uint16_t* aType) {
277 NS_ENSURE_ARG_POINTER(aType);
279 *aType = imgIContainer::TYPE_RASTER;
280 return NS_OK;
283 NS_IMETHODIMP
284 RasterImage::GetProviderId(uint32_t* aId) {
285 NS_ENSURE_ARG_POINTER(aId);
287 *aId = ImageResource::GetImageProviderId();
288 return NS_OK;
291 LookupResult RasterImage::LookupFrameInternal(const OrientedIntSize& aSize,
292 uint32_t aFlags,
293 PlaybackType aPlaybackType,
294 bool aMarkUsed) {
295 if (mAnimationState && aPlaybackType == PlaybackType::eAnimated) {
296 MOZ_ASSERT(mFrameAnimator);
297 MOZ_ASSERT(ToSurfaceFlags(aFlags) == DefaultSurfaceFlags(),
298 "Can't composite frames with non-default surface flags");
299 return mFrameAnimator->GetCompositedFrame(*mAnimationState, aMarkUsed);
302 SurfaceFlags surfaceFlags = ToSurfaceFlags(aFlags);
304 // We don't want any substitution for sync decodes, and substitution would be
305 // illegal when high quality downscaling is disabled, so we use
306 // SurfaceCache::Lookup in this case.
307 if ((aFlags & FLAG_SYNC_DECODE) || !(aFlags & FLAG_HIGH_QUALITY_SCALING)) {
308 return SurfaceCache::Lookup(
309 ImageKey(this),
310 RasterSurfaceKey(aSize.ToUnknownSize(), surfaceFlags,
311 PlaybackType::eStatic),
312 aMarkUsed);
315 // We'll return the best match we can find to the requested frame.
316 return SurfaceCache::LookupBestMatch(
317 ImageKey(this),
318 RasterSurfaceKey(aSize.ToUnknownSize(), surfaceFlags,
319 PlaybackType::eStatic),
320 aMarkUsed);
323 LookupResult RasterImage::LookupFrame(const OrientedIntSize& aSize,
324 uint32_t aFlags,
325 PlaybackType aPlaybackType,
326 bool aMarkUsed) {
327 MOZ_ASSERT(NS_IsMainThread());
329 // If we're opaque, we don't need to care about premultiplied alpha, because
330 // that can only matter for frames with transparency.
331 if (IsOpaque()) {
332 aFlags &= ~FLAG_DECODE_NO_PREMULTIPLY_ALPHA;
335 OrientedIntSize requestedSize =
336 CanDownscaleDuringDecode(aSize, aFlags) ? aSize : mSize;
337 if (requestedSize.IsEmpty()) {
338 // Can't decode to a surface of zero size.
339 return LookupResult(MatchType::NOT_FOUND);
342 LookupResult result =
343 LookupFrameInternal(requestedSize, aFlags, aPlaybackType, aMarkUsed);
345 if (!result && !LoadHasSize()) {
346 // We can't request a decode without knowing our intrinsic size. Give up.
347 return LookupResult(MatchType::NOT_FOUND);
350 // We want to trigger a decode if and only if:
351 // 1) There is no pending decode
352 // 2) There is no acceptable size decoded
353 // 3) The pending decode has not produced a frame yet, a sync decode is
354 // requested, and we have all the source data. Without the source data, we
355 // will just trigger another async decode anyways.
357 // TODO(aosmond): We should better handle case 3. We should actually return
358 // TEMPORARY_ERROR or NOT_READY if we don't have all the source data and a
359 // sync decode is requested. If there is a pending decode and we have all the
360 // source data, we should always be able to block on the frame's monitor --
361 // perhaps this could be accomplished by preallocating the first frame buffer
362 // when we create the decoder.
363 const bool syncDecode = aFlags & FLAG_SYNC_DECODE;
364 const bool avoidRedecode = aFlags & FLAG_AVOID_REDECODE_FOR_SIZE;
365 if (result.Type() == MatchType::NOT_FOUND ||
366 (result.Type() == MatchType::SUBSTITUTE_BECAUSE_NOT_FOUND &&
367 !avoidRedecode) ||
368 (syncDecode && !avoidRedecode && !result && LoadAllSourceData())) {
369 // We don't have a copy of this frame, and there's no decoder working on
370 // one. (Or we're sync decoding and the existing decoder hasn't even started
371 // yet.) Trigger decoding so it'll be available next time.
372 MOZ_ASSERT(aPlaybackType != PlaybackType::eAnimated ||
373 StaticPrefs::image_mem_animated_discardable_AtStartup() ||
374 !mAnimationState || mAnimationState->KnownFrameCount() < 1,
375 "Animated frames should be locked");
377 // The surface cache may suggest the preferred size we are supposed to
378 // decode at. This should only happen if we accept substitutions.
379 if (!result.SuggestedSize().IsEmpty()) {
380 MOZ_ASSERT(!syncDecode && (aFlags & FLAG_HIGH_QUALITY_SCALING));
381 requestedSize = OrientedIntSize::FromUnknownSize(result.SuggestedSize());
384 bool ranSync = false, failed = false;
385 Decode(requestedSize, aFlags, aPlaybackType, ranSync, failed);
386 if (failed) {
387 result.SetFailedToRequestDecode();
390 // If we can or did sync decode, we should already have the frame.
391 if (ranSync || syncDecode) {
392 result =
393 LookupFrameInternal(requestedSize, aFlags, aPlaybackType, aMarkUsed);
397 if (!result) {
398 // We still weren't able to get a frame. Give up.
399 return result;
402 // Sync decoding guarantees that we got the frame, but if it's owned by an
403 // async decoder that's currently running, the contents of the frame may not
404 // be available yet. Make sure we get everything.
405 if (LoadAllSourceData() && syncDecode) {
406 result.Surface()->WaitUntilFinished();
409 // If we could have done some decoding in this function we need to check if
410 // that decoding encountered an error and hence aborted the surface. We want
411 // to avoid calling IsAborted if we weren't passed any sync decode flag
412 // because IsAborted acquires the monitor for the imgFrame.
413 if (aFlags & (FLAG_SYNC_DECODE | FLAG_SYNC_DECODE_IF_FAST) &&
414 result.Surface()->IsAborted()) {
415 DrawableSurface tmp = std::move(result.Surface());
416 return result;
419 return result;
422 bool RasterImage::IsOpaque() {
423 if (mError) {
424 return false;
427 Progress progress = mProgressTracker->GetProgress();
429 // If we haven't yet finished decoding, the safe answer is "not opaque".
430 if (!(progress & FLAG_DECODE_COMPLETE)) {
431 return false;
434 // Other, we're opaque if FLAG_HAS_TRANSPARENCY is not set.
435 return !(progress & FLAG_HAS_TRANSPARENCY);
438 NS_IMETHODIMP_(bool)
439 RasterImage::WillDrawOpaqueNow() {
440 if (!IsOpaque()) {
441 return false;
444 if (mAnimationState) {
445 if (!StaticPrefs::image_mem_animated_discardable_AtStartup()) {
446 // We never discard frames of animated images.
447 return true;
448 } else {
449 if (mAnimationState->GetCompositedFrameInvalid()) {
450 // We're not going to draw anything at all.
451 return false;
456 // If we are not locked our decoded data could get discard at any time (ie
457 // between the call to this function and when we are asked to draw), so we
458 // have to return false if we are unlocked.
459 if (mLockCount == 0) {
460 return false;
463 LookupResult result = SurfaceCache::LookupBestMatch(
464 ImageKey(this),
465 RasterSurfaceKey(mSize.ToUnknownSize(), DefaultSurfaceFlags(),
466 PlaybackType::eStatic),
467 /* aMarkUsed = */ false);
468 MatchType matchType = result.Type();
469 if (matchType == MatchType::NOT_FOUND || matchType == MatchType::PENDING ||
470 !result.Surface()->IsFinished()) {
471 return false;
474 return true;
477 void RasterImage::OnSurfaceDiscarded(const SurfaceKey& aSurfaceKey) {
478 MOZ_ASSERT(mProgressTracker);
480 bool animatedFramesDiscarded =
481 mAnimationState && aSurfaceKey.Playback() == PlaybackType::eAnimated;
483 nsCOMPtr<nsIEventTarget> eventTarget = do_GetMainThread();
485 RefPtr<RasterImage> image = this;
486 nsCOMPtr<nsIRunnable> ev =
487 NS_NewRunnableFunction("RasterImage::OnSurfaceDiscarded", [=]() -> void {
488 image->OnSurfaceDiscardedInternal(animatedFramesDiscarded);
490 eventTarget->Dispatch(ev.forget(), NS_DISPATCH_NORMAL);
493 void RasterImage::OnSurfaceDiscardedInternal(bool aAnimatedFramesDiscarded) {
494 MOZ_ASSERT(NS_IsMainThread());
496 if (aAnimatedFramesDiscarded && mAnimationState) {
497 MOZ_ASSERT(StaticPrefs::image_mem_animated_discardable_AtStartup());
499 IntRect rect = mAnimationState->UpdateState(this, mSize.ToUnknownSize());
501 auto dirtyRect = OrientedIntRect::FromUnknownRect(rect);
502 NotifyProgress(NoProgress, dirtyRect);
505 if (mProgressTracker) {
506 mProgressTracker->OnDiscard();
510 //******************************************************************************
511 NS_IMETHODIMP
512 RasterImage::GetAnimated(bool* aAnimated) {
513 if (mError) {
514 return NS_ERROR_FAILURE;
517 NS_ENSURE_ARG_POINTER(aAnimated);
519 // If we have an AnimationState, we can know for sure.
520 if (mAnimationState) {
521 *aAnimated = true;
522 return NS_OK;
525 // Otherwise, we need to have been decoded to know for sure, since if we were
526 // decoded at least once mAnimationState would have been created for animated
527 // images. This is true even though we check for animation during the
528 // metadata decode, because we may still discover animation only during the
529 // full decode for corrupt images.
530 if (!LoadHasBeenDecoded()) {
531 return NS_ERROR_NOT_AVAILABLE;
534 // We know for sure
535 *aAnimated = false;
537 return NS_OK;
540 //******************************************************************************
541 NS_IMETHODIMP_(int32_t)
542 RasterImage::GetFirstFrameDelay() {
543 if (mError) {
544 return -1;
547 bool animated = false;
548 if (NS_FAILED(GetAnimated(&animated)) || !animated) {
549 return -1;
552 MOZ_ASSERT(mAnimationState, "Animated images should have an AnimationState");
553 return mAnimationState->FirstFrameTimeout().AsEncodedValueDeprecated();
556 NS_IMETHODIMP_(already_AddRefed<SourceSurface>)
557 RasterImage::GetFrame(uint32_t aWhichFrame, uint32_t aFlags) {
558 return GetFrameAtSize(mSize.ToUnknownSize(), aWhichFrame, aFlags);
561 NS_IMETHODIMP_(already_AddRefed<SourceSurface>)
562 RasterImage::GetFrameAtSize(const IntSize& aSize, uint32_t aWhichFrame,
563 uint32_t aFlags) {
564 MOZ_ASSERT(aWhichFrame <= FRAME_MAX_VALUE);
566 AutoProfilerImagePaintMarker PROFILER_RAII(this);
567 #ifdef DEBUG
568 NotifyDrawingObservers();
569 #endif
571 if (aSize.IsEmpty() || aWhichFrame > FRAME_MAX_VALUE || mError) {
572 return nullptr;
575 auto size = OrientedIntSize::FromUnknownSize(aSize);
577 // Get the frame. If it's not there, it's probably the caller's fault for
578 // not waiting for the data to be loaded from the network or not passing
579 // FLAG_SYNC_DECODE.
580 LookupResult result = LookupFrame(size, aFlags, ToPlaybackType(aWhichFrame),
581 /* aMarkUsed = */ true);
582 if (!result) {
583 // The OS threw this frame away and we couldn't redecode it.
584 return nullptr;
587 return result.Surface()->GetSourceSurface();
590 NS_IMETHODIMP_(bool)
591 RasterImage::IsImageContainerAvailable(WindowRenderer* aRenderer,
592 uint32_t aFlags) {
593 return LoadHasSize();
596 NS_IMETHODIMP_(ImgDrawResult)
597 RasterImage::GetImageProvider(WindowRenderer* aRenderer,
598 const gfx::IntSize& aSize,
599 const SVGImageContext& aSVGContext,
600 const Maybe<ImageIntRegion>& aRegion,
601 uint32_t aFlags,
602 WebRenderImageProvider** aProvider) {
603 MOZ_ASSERT(NS_IsMainThread());
604 MOZ_ASSERT(aRenderer);
606 if (mError) {
607 return ImgDrawResult::BAD_IMAGE;
610 if (!LoadHasSize()) {
611 return ImgDrawResult::NOT_READY;
614 if (aSize.IsEmpty()) {
615 return ImgDrawResult::BAD_ARGS;
618 // We check the minimum size because while we support downscaling, we do not
619 // support upscaling. If aRequestedSize > mSize, we will never give a larger
620 // surface than mSize. If mSize > aRequestedSize, and mSize > maxTextureSize,
621 // we still want to use image containers if aRequestedSize <= maxTextureSize.
622 int32_t maxTextureSize = aRenderer->GetMaxTextureSize();
623 if (min(mSize.width, aSize.width) > maxTextureSize ||
624 min(mSize.height, aSize.height) > maxTextureSize) {
625 return ImgDrawResult::NOT_SUPPORTED;
628 AutoProfilerImagePaintMarker PROFILER_RAII(this);
629 #ifdef DEBUG
630 NotifyDrawingObservers();
631 #endif
633 // Get the frame. If it's not there, it's probably the caller's fault for
634 // not waiting for the data to be loaded from the network or not passing
635 // FLAG_SYNC_DECODE.
636 LookupResult result = LookupFrame(OrientedIntSize::FromUnknownSize(aSize),
637 aFlags, PlaybackType::eAnimated,
638 /* aMarkUsed = */ true);
639 if (!result) {
640 // The OS threw this frame away and we couldn't redecode it.
641 return ImgDrawResult::NOT_READY;
644 if (!result.Surface()->IsFinished()) {
645 result.Surface().TakeProvider(aProvider);
646 return ImgDrawResult::INCOMPLETE;
649 result.Surface().TakeProvider(aProvider);
650 switch (result.Type()) {
651 case MatchType::SUBSTITUTE_BECAUSE_NOT_FOUND:
652 case MatchType::SUBSTITUTE_BECAUSE_PENDING:
653 return ImgDrawResult::WRONG_SIZE;
654 default:
655 return ImgDrawResult::SUCCESS;
659 size_t RasterImage::SizeOfSourceWithComputedFallback(
660 SizeOfState& aState) const {
661 return mSourceBuffer->SizeOfIncludingThisWithComputedFallback(
662 aState.mMallocSizeOf);
665 bool RasterImage::SetMetadata(const ImageMetadata& aMetadata,
666 bool aFromMetadataDecode) {
667 MOZ_ASSERT(NS_IsMainThread());
669 if (mError) {
670 return true;
673 mResolution = aMetadata.GetResolution();
675 if (aMetadata.HasSize()) {
676 auto metadataSize = aMetadata.GetSize();
677 if (metadataSize.width < 0 || metadataSize.height < 0) {
678 NS_WARNING("Image has negative intrinsic size");
679 DoError();
680 return true;
683 MOZ_ASSERT(aMetadata.HasOrientation());
684 Orientation orientation = aMetadata.GetOrientation();
686 // If we already have a size, check the new size against the old one.
687 if (LoadHasSize() &&
688 (metadataSize != mSize || orientation != mOrientation)) {
689 NS_WARNING(
690 "Image changed size or orientation on redecode! "
691 "This should not happen!");
692 DoError();
693 return true;
696 // Set the size and flag that we have it.
697 mOrientation = orientation;
698 mSize = metadataSize;
699 mNativeSizes.Clear();
700 for (const auto& nativeSize : aMetadata.GetNativeSizes()) {
701 mNativeSizes.AppendElement(nativeSize);
703 StoreHasSize(true);
706 if (LoadHasSize() && aMetadata.HasAnimation() && !mAnimationState) {
707 // We're becoming animated, so initialize animation stuff.
708 mAnimationState.emplace(mAnimationMode);
709 mFrameAnimator = MakeUnique<FrameAnimator>(this, mSize.ToUnknownSize());
711 if (!StaticPrefs::image_mem_animated_discardable_AtStartup()) {
712 // We don't support discarding animated images (See bug 414259).
713 // Lock the image and throw away the key.
714 LockImage();
717 if (!aFromMetadataDecode) {
718 // The metadata decode reported that this image isn't animated, but we
719 // discovered that it actually was during the full decode. This is a
720 // rare failure that only occurs for corrupt images. To recover, we need
721 // to discard all existing surfaces and redecode.
722 return false;
726 if (mAnimationState) {
727 mAnimationState->SetLoopCount(aMetadata.GetLoopCount());
728 mAnimationState->SetFirstFrameTimeout(aMetadata.GetFirstFrameTimeout());
730 if (aMetadata.HasLoopLength()) {
731 mAnimationState->SetLoopLength(aMetadata.GetLoopLength());
733 if (aMetadata.HasFirstFrameRefreshArea()) {
734 mAnimationState->SetFirstFrameRefreshArea(
735 aMetadata.GetFirstFrameRefreshArea());
739 if (aMetadata.HasHotspot()) {
740 // NOTE(heycam): We shouldn't have any image formats that support both
741 // orientation and hotspots, so we assert that rather than add code
742 // to orient the hotspot point correctly.
743 MOZ_ASSERT(mOrientation.IsIdentity(), "Would need to orient hotspot point");
745 auto hotspot = aMetadata.GetHotspot();
746 mHotspot.x = std::max(std::min(hotspot.x.value, mSize.width - 1), 0);
747 mHotspot.y = std::max(std::min(hotspot.y.value, mSize.height - 1), 0);
750 return true;
753 NS_IMETHODIMP
754 RasterImage::SetAnimationMode(uint16_t aAnimationMode) {
755 if (mAnimationState) {
756 mAnimationState->SetAnimationMode(aAnimationMode);
758 return SetAnimationModeInternal(aAnimationMode);
761 //******************************************************************************
763 nsresult RasterImage::StartAnimation() {
764 if (mError) {
765 return NS_ERROR_FAILURE;
768 MOZ_ASSERT(ShouldAnimate(), "Should not animate!");
770 // If we're not ready to animate, then set mPendingAnimation, which will cause
771 // us to start animating if and when we do become ready.
772 StorePendingAnimation(!mAnimationState ||
773 mAnimationState->KnownFrameCount() < 1);
774 if (LoadPendingAnimation()) {
775 return NS_OK;
778 // Don't bother to animate if we're displaying the first frame forever.
779 if (mAnimationState->GetCurrentAnimationFrameIndex() == 0 &&
780 mAnimationState->FirstFrameTimeout() == FrameTimeout::Forever()) {
781 StoreAnimationFinished(true);
782 return NS_ERROR_ABORT;
785 // We need to set the time that this initial frame was first displayed, as
786 // this is used in AdvanceFrame().
787 mAnimationState->InitAnimationFrameTimeIfNecessary();
789 return NS_OK;
792 //******************************************************************************
793 nsresult RasterImage::StopAnimation() {
794 MOZ_ASSERT(mAnimating, "Should be animating!");
796 nsresult rv = NS_OK;
797 if (mError) {
798 rv = NS_ERROR_FAILURE;
799 } else {
800 mAnimationState->SetAnimationFrameTime(TimeStamp());
803 mAnimating = false;
804 return rv;
807 //******************************************************************************
808 NS_IMETHODIMP
809 RasterImage::ResetAnimation() {
810 if (mError) {
811 return NS_ERROR_FAILURE;
814 StorePendingAnimation(false);
816 if (mAnimationMode == kDontAnimMode || !mAnimationState ||
817 mAnimationState->GetCurrentAnimationFrameIndex() == 0) {
818 return NS_OK;
821 StoreAnimationFinished(false);
823 if (mAnimating) {
824 StopAnimation();
827 MOZ_ASSERT(mAnimationState, "Should have AnimationState");
828 MOZ_ASSERT(mFrameAnimator, "Should have FrameAnimator");
829 mFrameAnimator->ResetAnimation(*mAnimationState);
831 IntRect area = mAnimationState->FirstFrameRefreshArea();
832 NotifyProgress(NoProgress, OrientedIntRect::FromUnknownRect(area));
834 // Start the animation again. It may not have been running before, if
835 // mAnimationFinished was true before entering this function.
836 EvaluateAnimation();
838 return NS_OK;
841 //******************************************************************************
842 NS_IMETHODIMP_(void)
843 RasterImage::SetAnimationStartTime(const TimeStamp& aTime) {
844 if (mError || mAnimationMode == kDontAnimMode || mAnimating ||
845 !mAnimationState) {
846 return;
849 mAnimationState->SetAnimationFrameTime(aTime);
852 NS_IMETHODIMP_(float)
853 RasterImage::GetFrameIndex(uint32_t aWhichFrame) {
854 MOZ_ASSERT(aWhichFrame <= FRAME_MAX_VALUE, "Invalid argument");
855 return (aWhichFrame == FRAME_FIRST || !mAnimationState)
856 ? 0.0f
857 : mAnimationState->GetCurrentAnimationFrameIndex();
860 NS_IMETHODIMP_(IntRect)
861 RasterImage::GetImageSpaceInvalidationRect(const IntRect& aRect) {
862 // Note that we do not transform aRect into an UnorientedIntRect, since
863 // RasterImage::NotifyProgress notifies all consumers of the image using
864 // OrientedIntRect values. (This is unlike OrientedImage, which notifies
865 // using inner image coordinates.)
866 return aRect;
869 nsresult RasterImage::OnImageDataComplete(nsIRequest*, nsresult aStatus,
870 bool aLastPart) {
871 MOZ_ASSERT(NS_IsMainThread());
873 // Record that we have all the data we're going to get now.
874 StoreAllSourceData(true);
876 // Let decoders know that there won't be any more data coming.
877 mSourceBuffer->Complete(aStatus);
879 // Allow a synchronous metadata decode if mSyncLoad was set, or if we're
880 // running on a single thread (in which case waiting for the async metadata
881 // decoder could delay this image's load event quite a bit), or if this image
882 // is transient.
883 bool canSyncDecodeMetadata =
884 LoadSyncLoad() || LoadTransient() || DecodePool::NumberOfCores() < 2;
886 if (canSyncDecodeMetadata && !LoadHasSize()) {
887 // We're loading this image synchronously, so it needs to be usable after
888 // this call returns. Since we haven't gotten our size yet, we need to do a
889 // synchronous metadata decode here.
890 DecodeMetadata(FLAG_SYNC_DECODE);
893 // Determine our final status, giving precedence to Necko failure codes. We
894 // check after running the metadata decode in case it triggered an error.
895 nsresult finalStatus = mError ? NS_ERROR_FAILURE : NS_OK;
896 if (NS_FAILED(aStatus)) {
897 finalStatus = aStatus;
900 // If loading failed, report an error.
901 if (NS_FAILED(finalStatus)) {
902 DoError();
905 Progress loadProgress = LoadCompleteProgress(aLastPart, mError, finalStatus);
907 if (!LoadHasSize() && !mError) {
908 // We don't have our size yet, so we'll fire the load event in SetSize().
909 MOZ_ASSERT(!canSyncDecodeMetadata,
910 "Firing load async after metadata sync decode?");
911 mLoadProgress = Some(loadProgress);
912 return finalStatus;
915 NotifyForLoadEvent(loadProgress);
917 return finalStatus;
920 void RasterImage::NotifyForLoadEvent(Progress aProgress) {
921 MOZ_ASSERT(LoadHasSize() || mError,
922 "Need to know size before firing load event");
923 MOZ_ASSERT(
924 !LoadHasSize() || (mProgressTracker->GetProgress() & FLAG_SIZE_AVAILABLE),
925 "Should have notified that the size is available if we have it");
927 // If we encountered an error, make sure we notify for that as well.
928 if (mError) {
929 aProgress |= FLAG_HAS_ERROR;
932 // Notify our listeners, which will fire this image's load event.
933 NotifyProgress(aProgress);
936 nsresult RasterImage::OnImageDataAvailable(nsIRequest*,
937 nsIInputStream* aInputStream,
938 uint64_t, uint32_t aCount) {
939 nsresult rv = mSourceBuffer->AppendFromInputStream(aInputStream, aCount);
940 if (NS_SUCCEEDED(rv) && !LoadSomeSourceData()) {
941 StoreSomeSourceData(true);
942 if (!LoadSyncLoad()) {
943 // Create an async metadata decoder and verify we succeed in doing so.
944 rv = DecodeMetadata(DECODE_FLAGS_DEFAULT);
948 if (NS_FAILED(rv)) {
949 DoError();
951 return rv;
954 nsresult RasterImage::SetSourceSizeHint(uint32_t aSizeHint) {
955 if (aSizeHint == 0) {
956 return NS_OK;
959 nsresult rv = mSourceBuffer->ExpectLength(aSizeHint);
960 if (rv == NS_ERROR_OUT_OF_MEMORY) {
961 // Flush memory, try to get some back, and try again.
962 rv = nsMemory::HeapMinimize(true);
963 if (NS_SUCCEEDED(rv)) {
964 rv = mSourceBuffer->ExpectLength(aSizeHint);
968 return rv;
971 nsresult RasterImage::GetHotspotX(int32_t* aX) {
972 *aX = mHotspot.x;
973 return NS_OK;
976 nsresult RasterImage::GetHotspotY(int32_t* aY) {
977 *aY = mHotspot.y;
978 return NS_OK;
981 void RasterImage::Discard() {
982 MOZ_ASSERT(NS_IsMainThread());
983 MOZ_ASSERT(CanDiscard(), "Asked to discard but can't");
984 MOZ_ASSERT(!mAnimationState ||
985 StaticPrefs::image_mem_animated_discardable_AtStartup(),
986 "Asked to discard for animated image");
988 // Delete all the decoded frames.
989 SurfaceCache::RemoveImage(ImageKey(this));
991 if (mAnimationState) {
992 IntRect rect = mAnimationState->UpdateState(this, mSize.ToUnknownSize());
994 auto dirtyRect = OrientedIntRect::FromUnknownRect(rect);
995 NotifyProgress(NoProgress, dirtyRect);
998 // Notify that we discarded.
999 if (mProgressTracker) {
1000 mProgressTracker->OnDiscard();
1004 bool RasterImage::CanDiscard() {
1005 return LoadAllSourceData() &&
1006 // Can discard animated images if the pref is set
1007 (!mAnimationState ||
1008 StaticPrefs::image_mem_animated_discardable_AtStartup());
1011 NS_IMETHODIMP
1012 RasterImage::StartDecoding(uint32_t aFlags, uint32_t aWhichFrame) {
1013 if (mError) {
1014 return NS_ERROR_FAILURE;
1017 if (!LoadHasSize()) {
1018 StoreWantFullDecode(true);
1019 return NS_OK;
1022 uint32_t flags = (aFlags & FLAG_ASYNC_NOTIFY) | FLAG_SYNC_DECODE_IF_FAST |
1023 FLAG_HIGH_QUALITY_SCALING;
1024 return RequestDecodeForSize(mSize.ToUnknownSize(), flags, aWhichFrame);
1027 bool RasterImage::StartDecodingWithResult(uint32_t aFlags,
1028 uint32_t aWhichFrame) {
1029 if (mError) {
1030 return false;
1033 if (!LoadHasSize()) {
1034 StoreWantFullDecode(true);
1035 return false;
1038 uint32_t flags = (aFlags & FLAG_ASYNC_NOTIFY) | FLAG_SYNC_DECODE_IF_FAST |
1039 FLAG_HIGH_QUALITY_SCALING;
1040 LookupResult result = RequestDecodeForSizeInternal(mSize, flags, aWhichFrame);
1041 DrawableSurface surface = std::move(result.Surface());
1042 return surface && surface->IsFinished();
1045 bool RasterImage::HasDecodedPixels() {
1046 LookupResult result = SurfaceCache::LookupBestMatch(
1047 ImageKey(this),
1048 RasterSurfaceKey(mSize.ToUnknownSize(), DefaultSurfaceFlags(),
1049 PlaybackType::eStatic),
1050 /* aMarkUsed = */ false);
1051 MatchType matchType = result.Type();
1052 if (matchType == MatchType::NOT_FOUND || matchType == MatchType::PENDING ||
1053 !bool(result.Surface())) {
1054 return false;
1057 return !result.Surface()->GetDecodedRect().IsEmpty();
1060 imgIContainer::DecodeResult RasterImage::RequestDecodeWithResult(
1061 uint32_t aFlags, uint32_t aWhichFrame) {
1062 MOZ_ASSERT(NS_IsMainThread());
1064 if (mError) {
1065 return imgIContainer::DECODE_REQUEST_FAILED;
1068 uint32_t flags = aFlags | FLAG_ASYNC_NOTIFY;
1069 LookupResult result = RequestDecodeForSizeInternal(mSize, flags, aWhichFrame);
1070 DrawableSurface surface = std::move(result.Surface());
1071 if (surface && surface->IsFinished()) {
1072 return imgIContainer::DECODE_SURFACE_AVAILABLE;
1074 if (result.GetFailedToRequestDecode()) {
1075 return imgIContainer::DECODE_REQUEST_FAILED;
1077 return imgIContainer::DECODE_REQUESTED;
1080 NS_IMETHODIMP
1081 RasterImage::RequestDecodeForSize(const IntSize& aSize, uint32_t aFlags,
1082 uint32_t aWhichFrame) {
1083 MOZ_ASSERT(NS_IsMainThread());
1085 if (mError) {
1086 return NS_ERROR_FAILURE;
1089 RequestDecodeForSizeInternal(OrientedIntSize::FromUnknownSize(aSize), aFlags,
1090 aWhichFrame);
1092 return NS_OK;
1095 LookupResult RasterImage::RequestDecodeForSizeInternal(
1096 const OrientedIntSize& aSize, uint32_t aFlags, uint32_t aWhichFrame) {
1097 MOZ_ASSERT(NS_IsMainThread());
1099 if (aWhichFrame > FRAME_MAX_VALUE) {
1100 return LookupResult(MatchType::NOT_FOUND);
1103 if (mError) {
1104 LookupResult result = LookupResult(MatchType::NOT_FOUND);
1105 result.SetFailedToRequestDecode();
1106 return result;
1109 if (!LoadHasSize()) {
1110 StoreWantFullDecode(true);
1111 return LookupResult(MatchType::NOT_FOUND);
1114 // Decide whether to sync decode images we can decode quickly. Here we are
1115 // explicitly trading off flashing for responsiveness in the case that we're
1116 // redecoding an image (see bug 845147).
1117 bool shouldSyncDecodeIfFast =
1118 !LoadHasBeenDecoded() && (aFlags & FLAG_SYNC_DECODE_IF_FAST);
1120 uint32_t flags =
1121 shouldSyncDecodeIfFast ? aFlags : aFlags & ~FLAG_SYNC_DECODE_IF_FAST;
1123 // Perform a frame lookup, which will implicitly start decoding if needed.
1124 return LookupFrame(aSize, flags, ToPlaybackType(aWhichFrame),
1125 /* aMarkUsed = */ false);
1128 static bool LaunchDecodingTask(IDecodingTask* aTask, RasterImage* aImage,
1129 uint32_t aFlags, bool aHaveSourceData) {
1130 if (aHaveSourceData) {
1131 nsCString uri(aImage->GetURIString());
1133 // If we have all the data, we can sync decode if requested.
1134 if (aFlags & imgIContainer::FLAG_SYNC_DECODE) {
1135 DecodePool::Singleton()->SyncRunIfPossible(aTask, uri);
1136 return true;
1139 if (aFlags & imgIContainer::FLAG_SYNC_DECODE_IF_FAST) {
1140 return DecodePool::Singleton()->SyncRunIfPreferred(aTask, uri);
1144 // Perform an async decode. We also take this path if we don't have all the
1145 // source data yet, since sync decoding is impossible in that situation.
1146 DecodePool::Singleton()->AsyncRun(aTask);
1147 return false;
1150 void RasterImage::Decode(const OrientedIntSize& aSize, uint32_t aFlags,
1151 PlaybackType aPlaybackType, bool& aOutRanSync,
1152 bool& aOutFailed) {
1153 MOZ_ASSERT(NS_IsMainThread());
1155 if (mError) {
1156 aOutFailed = true;
1157 return;
1160 // If we don't have a size yet, we can't do any other decoding.
1161 if (!LoadHasSize()) {
1162 StoreWantFullDecode(true);
1163 return;
1166 // We're about to decode again, which may mean that some of the previous sizes
1167 // we've decoded at aren't useful anymore. We can allow them to expire from
1168 // the cache by unlocking them here. When the decode finishes, it will send an
1169 // invalidation that will cause all instances of this image to redraw. If this
1170 // image is locked, any surfaces that are still useful will become locked
1171 // again when LookupFrame touches them, and the remainder will eventually
1172 // expire.
1173 SurfaceCache::UnlockEntries(ImageKey(this));
1175 // Determine which flags we need to decode this image with.
1176 DecoderFlags decoderFlags = mDefaultDecoderFlags;
1177 if (aFlags & FLAG_ASYNC_NOTIFY) {
1178 decoderFlags |= DecoderFlags::ASYNC_NOTIFY;
1180 if (LoadTransient()) {
1181 decoderFlags |= DecoderFlags::IMAGE_IS_TRANSIENT;
1183 if (LoadHasBeenDecoded()) {
1184 decoderFlags |= DecoderFlags::IS_REDECODE;
1186 if ((aFlags & FLAG_SYNC_DECODE) || !(aFlags & FLAG_HIGH_QUALITY_SCALING)) {
1187 // Used SurfaceCache::Lookup instead of SurfaceCache::LookupBestMatch. That
1188 // means the caller can handle a differently sized surface to be returned
1189 // at any point.
1190 decoderFlags |= DecoderFlags::CANNOT_SUBSTITUTE;
1193 SurfaceFlags surfaceFlags = ToSurfaceFlags(aFlags);
1194 if (IsOpaque()) {
1195 // If there's no transparency, it doesn't matter whether we premultiply
1196 // alpha or not.
1197 surfaceFlags &= ~SurfaceFlags::NO_PREMULTIPLY_ALPHA;
1200 // Create a decoder.
1201 RefPtr<IDecodingTask> task;
1202 nsresult rv;
1203 bool animated = mAnimationState && aPlaybackType == PlaybackType::eAnimated;
1204 if (animated) {
1205 size_t currentFrame = mAnimationState->GetCurrentAnimationFrameIndex();
1206 rv = DecoderFactory::CreateAnimationDecoder(
1207 mDecoderType, WrapNotNull(this), mSourceBuffer, mSize.ToUnknownSize(),
1208 decoderFlags, surfaceFlags, currentFrame, getter_AddRefs(task));
1209 } else {
1210 rv = DecoderFactory::CreateDecoder(mDecoderType, WrapNotNull(this),
1211 mSourceBuffer, mSize.ToUnknownSize(),
1212 aSize.ToUnknownSize(), decoderFlags,
1213 surfaceFlags, getter_AddRefs(task));
1216 if (rv == NS_ERROR_ALREADY_INITIALIZED) {
1217 // We raced with an already pending decoder, and it finished before we
1218 // managed to insert the new decoder. Pretend we did a sync call to make
1219 // the caller lookup in the surface cache again.
1220 MOZ_ASSERT(!task);
1221 aOutRanSync = true;
1222 return;
1225 if (animated) {
1226 // We pass false for aAllowInvalidation because we may be asked to use
1227 // async notifications. Any potential invalidation here will be sent when
1228 // RequestRefresh is called, or NotifyDecodeComplete.
1229 #ifdef DEBUG
1230 IntRect rect =
1231 #endif
1232 mAnimationState->UpdateState(this, mSize.ToUnknownSize(), false);
1233 MOZ_ASSERT(rect.IsEmpty());
1236 // Make sure DecoderFactory was able to create a decoder successfully.
1237 if (NS_FAILED(rv)) {
1238 MOZ_ASSERT(!task);
1239 aOutFailed = true;
1240 return;
1243 MOZ_ASSERT(task);
1244 mDecodeCount++;
1246 // We're ready to decode; start the decoder.
1247 aOutRanSync = LaunchDecodingTask(task, this, aFlags, LoadAllSourceData());
1250 NS_IMETHODIMP
1251 RasterImage::DecodeMetadata(uint32_t aFlags) {
1252 if (mError) {
1253 return NS_ERROR_FAILURE;
1256 MOZ_ASSERT(!LoadHasSize(), "Should not do unnecessary metadata decodes");
1258 // Create a decoder.
1259 RefPtr<IDecodingTask> task = DecoderFactory::CreateMetadataDecoder(
1260 mDecoderType, WrapNotNull(this), mDefaultDecoderFlags, mSourceBuffer);
1262 // Make sure DecoderFactory was able to create a decoder successfully.
1263 if (!task) {
1264 return NS_ERROR_FAILURE;
1267 // We're ready to decode; start the decoder.
1268 LaunchDecodingTask(task, this, aFlags, LoadAllSourceData());
1269 return NS_OK;
1272 void RasterImage::RecoverFromInvalidFrames(const OrientedIntSize& aSize,
1273 uint32_t aFlags) {
1274 if (!LoadHasSize()) {
1275 return;
1278 NS_WARNING("A RasterImage's frames became invalid. Attempting to recover...");
1280 // Discard all existing frames, since they're probably all now invalid.
1281 SurfaceCache::RemoveImage(ImageKey(this));
1283 // Relock the image if it's supposed to be locked.
1284 if (mLockCount > 0) {
1285 SurfaceCache::LockImage(ImageKey(this));
1288 bool unused1, unused2;
1290 // Animated images require some special handling, because we normally require
1291 // that they never be discarded.
1292 if (mAnimationState) {
1293 Decode(mSize, aFlags | FLAG_SYNC_DECODE, PlaybackType::eAnimated, unused1,
1294 unused2);
1295 ResetAnimation();
1296 return;
1299 // For non-animated images, it's fine to recover using an async decode.
1300 Decode(aSize, aFlags, PlaybackType::eStatic, unused1, unused2);
1303 bool RasterImage::CanDownscaleDuringDecode(const OrientedIntSize& aSize,
1304 uint32_t aFlags) {
1305 // Check basic requirements: downscale-during-decode is enabled, Skia is
1306 // available, this image isn't transient, we have all the source data and know
1307 // our size, and the flags allow us to do it.
1308 if (!LoadHasSize() || LoadTransient() ||
1309 !StaticPrefs::image_downscale_during_decode_enabled() ||
1310 !(aFlags & imgIContainer::FLAG_HIGH_QUALITY_SCALING)) {
1311 return false;
1314 // We don't downscale animated images during decode.
1315 if (mAnimationState) {
1316 return false;
1319 // Never upscale.
1320 if (aSize.width >= mSize.width || aSize.height >= mSize.height) {
1321 return false;
1324 // Zero or negative width or height is unacceptable.
1325 if (aSize.width < 1 || aSize.height < 1) {
1326 return false;
1329 // There's no point in scaling if we can't store the result.
1330 if (!SurfaceCache::CanHold(aSize.ToUnknownSize())) {
1331 return false;
1334 return true;
1337 ImgDrawResult RasterImage::DrawInternal(DrawableSurface&& aSurface,
1338 gfxContext* aContext,
1339 const OrientedIntSize& aSize,
1340 const ImageRegion& aRegion,
1341 SamplingFilter aSamplingFilter,
1342 uint32_t aFlags, float aOpacity) {
1343 gfxContextMatrixAutoSaveRestore saveMatrix(aContext);
1344 ImageRegion region(aRegion);
1345 bool frameIsFinished = aSurface->IsFinished();
1347 AutoProfilerImagePaintMarker PROFILER_RAII(this);
1348 #ifdef DEBUG
1349 NotifyDrawingObservers();
1350 #endif
1352 // By now we may have a frame with the requested size. If not, we need to
1353 // adjust the drawing parameters accordingly.
1354 IntSize finalSize = aSurface->GetSize();
1355 bool couldRedecodeForBetterFrame = false;
1356 if (finalSize != aSize.ToUnknownSize()) {
1357 gfx::MatrixScales scale(double(aSize.width) / finalSize.width,
1358 double(aSize.height) / finalSize.height);
1359 aContext->Multiply(gfx::Matrix::Scaling(scale));
1360 region.Scale(1.0 / scale.xScale, 1.0 / scale.yScale);
1362 couldRedecodeForBetterFrame = CanDownscaleDuringDecode(aSize, aFlags);
1365 if (!aSurface->Draw(aContext, region, aSamplingFilter, aFlags, aOpacity)) {
1366 RecoverFromInvalidFrames(aSize, aFlags);
1367 return ImgDrawResult::TEMPORARY_ERROR;
1369 if (!frameIsFinished) {
1370 return ImgDrawResult::INCOMPLETE;
1372 if (couldRedecodeForBetterFrame) {
1373 return ImgDrawResult::WRONG_SIZE;
1375 return ImgDrawResult::SUCCESS;
1378 //******************************************************************************
1379 NS_IMETHODIMP_(ImgDrawResult)
1380 RasterImage::Draw(gfxContext* aContext, const IntSize& aSize,
1381 const ImageRegion& aRegion, uint32_t aWhichFrame,
1382 SamplingFilter aSamplingFilter,
1383 const SVGImageContext& /*aSVGContext - ignored*/,
1384 uint32_t aFlags, float aOpacity) {
1385 if (aWhichFrame > FRAME_MAX_VALUE) {
1386 return ImgDrawResult::BAD_ARGS;
1389 if (mError) {
1390 return ImgDrawResult::BAD_IMAGE;
1393 // Illegal -- you can't draw with non-default decode flags.
1394 // (Disabling colorspace conversion might make sense to allow, but
1395 // we don't currently.)
1396 if (ToSurfaceFlags(aFlags) != DefaultSurfaceFlags()) {
1397 return ImgDrawResult::BAD_ARGS;
1400 if (!aContext) {
1401 return ImgDrawResult::BAD_ARGS;
1404 if (mAnimationConsumers == 0 && mAnimationState) {
1405 SendOnUnlockedDraw(aFlags);
1408 // If we're not using SamplingFilter::GOOD, we shouldn't high-quality scale or
1409 // downscale during decode.
1410 uint32_t flags = aSamplingFilter == SamplingFilter::GOOD
1411 ? aFlags
1412 : aFlags & ~FLAG_HIGH_QUALITY_SCALING;
1414 auto size = OrientedIntSize::FromUnknownSize(aSize);
1415 LookupResult result = LookupFrame(size, flags, ToPlaybackType(aWhichFrame),
1416 /* aMarkUsed = */ true);
1417 if (!result) {
1418 // Getting the frame (above) touches the image and kicks off decoding.
1419 if (mDrawStartTime.IsNull()) {
1420 mDrawStartTime = TimeStamp::Now();
1422 return ImgDrawResult::NOT_READY;
1425 bool shouldRecordTelemetry =
1426 !mDrawStartTime.IsNull() && result.Surface()->IsFinished();
1428 ImgDrawResult drawResult =
1429 DrawInternal(std::move(result.Surface()), aContext, size, aRegion,
1430 aSamplingFilter, flags, aOpacity);
1432 if (shouldRecordTelemetry) {
1433 TimeDuration drawLatency = TimeStamp::Now() - mDrawStartTime;
1434 Telemetry::Accumulate(Telemetry::IMAGE_DECODE_ON_DRAW_LATENCY,
1435 int32_t(drawLatency.ToMicroseconds()));
1436 mDrawStartTime = TimeStamp();
1439 return drawResult;
1442 //******************************************************************************
1444 NS_IMETHODIMP
1445 RasterImage::LockImage() {
1446 MOZ_ASSERT(NS_IsMainThread(),
1447 "Main thread to encourage serialization with UnlockImage");
1448 if (mError) {
1449 return NS_ERROR_FAILURE;
1452 // Increment the lock count
1453 mLockCount++;
1455 // Lock this image's surfaces in the SurfaceCache.
1456 if (mLockCount == 1) {
1457 SurfaceCache::LockImage(ImageKey(this));
1460 return NS_OK;
1463 //******************************************************************************
1465 NS_IMETHODIMP
1466 RasterImage::UnlockImage() {
1467 MOZ_ASSERT(NS_IsMainThread(),
1468 "Main thread to encourage serialization with LockImage");
1469 if (mError) {
1470 return NS_ERROR_FAILURE;
1473 // It's an error to call this function if the lock count is 0
1474 MOZ_ASSERT(mLockCount > 0, "Calling UnlockImage with mLockCount == 0!");
1475 if (mLockCount == 0) {
1476 return NS_ERROR_ABORT;
1479 // Decrement our lock count
1480 mLockCount--;
1482 // Unlock this image's surfaces in the SurfaceCache.
1483 if (mLockCount == 0) {
1484 SurfaceCache::UnlockImage(ImageKey(this));
1487 return NS_OK;
1490 //******************************************************************************
1492 NS_IMETHODIMP
1493 RasterImage::RequestDiscard() {
1494 if (LoadDiscardable() && // Enabled at creation time...
1495 mLockCount == 0 && // ...not temporarily disabled...
1496 CanDiscard()) {
1497 Discard();
1500 return NS_OK;
1503 // Idempotent error flagging routine. If a decoder is open, shuts it down.
1504 void RasterImage::DoError() {
1505 // If we've flagged an error before, we have nothing to do
1506 if (mError) {
1507 return;
1510 // We can't safely handle errors off-main-thread, so dispatch a worker to
1511 // do it.
1512 if (!NS_IsMainThread()) {
1513 HandleErrorWorker::DispatchIfNeeded(this);
1514 return;
1517 // Put the container in an error state.
1518 mError = true;
1520 // Stop animation and release our FrameAnimator.
1521 if (mAnimating) {
1522 StopAnimation();
1524 mAnimationState = Nothing();
1525 mFrameAnimator = nullptr;
1527 // Release all locks.
1528 mLockCount = 0;
1529 SurfaceCache::UnlockImage(ImageKey(this));
1531 // Release all frames from the surface cache.
1532 SurfaceCache::RemoveImage(ImageKey(this));
1534 // Invalidate to get rid of any partially-drawn image content.
1535 auto dirtyRect = OrientedIntRect({0, 0}, mSize);
1536 NotifyProgress(NoProgress, dirtyRect);
1538 MOZ_LOG(gImgLog, LogLevel::Error,
1539 ("RasterImage: [this=%p] Error detected for image\n", this));
1542 /* static */
1543 void RasterImage::HandleErrorWorker::DispatchIfNeeded(RasterImage* aImage) {
1544 RefPtr<HandleErrorWorker> worker = new HandleErrorWorker(aImage);
1545 NS_DispatchToMainThread(worker);
1548 RasterImage::HandleErrorWorker::HandleErrorWorker(RasterImage* aImage)
1549 : Runnable("image::RasterImage::HandleErrorWorker"), mImage(aImage) {
1550 MOZ_ASSERT(mImage, "Should have image");
1553 NS_IMETHODIMP
1554 RasterImage::HandleErrorWorker::Run() {
1555 mImage->DoError();
1557 return NS_OK;
1560 bool RasterImage::ShouldAnimate() {
1561 return ImageResource::ShouldAnimate() && mAnimationState &&
1562 mAnimationState->KnownFrameCount() >= 1 && !LoadAnimationFinished();
1565 #ifdef DEBUG
1566 NS_IMETHODIMP
1567 RasterImage::GetFramesNotified(uint32_t* aFramesNotified) {
1568 NS_ENSURE_ARG_POINTER(aFramesNotified);
1570 *aFramesNotified = mFramesNotified;
1572 return NS_OK;
1574 #endif
1576 void RasterImage::NotifyProgress(
1577 Progress aProgress,
1578 const OrientedIntRect& aInvalidRect /* = OrientedIntRect() */,
1579 const Maybe<uint32_t>& aFrameCount /* = Nothing() */,
1580 DecoderFlags aDecoderFlags /* = DefaultDecoderFlags() */,
1581 SurfaceFlags aSurfaceFlags /* = DefaultSurfaceFlags() */) {
1582 MOZ_ASSERT(NS_IsMainThread());
1584 // Ensure that we stay alive long enough to finish notifying.
1585 RefPtr<RasterImage> image = this;
1587 OrientedIntRect invalidRect = aInvalidRect;
1589 if (!(aDecoderFlags & DecoderFlags::FIRST_FRAME_ONLY)) {
1590 // We may have decoded new animation frames; update our animation state.
1591 MOZ_ASSERT_IF(aFrameCount && *aFrameCount > 1, mAnimationState || mError);
1592 if (mAnimationState && aFrameCount) {
1593 mAnimationState->UpdateKnownFrameCount(*aFrameCount);
1596 // If we should start animating right now, do so.
1597 if (mAnimationState && aFrameCount == Some(1u) && LoadPendingAnimation() &&
1598 ShouldAnimate()) {
1599 StartAnimation();
1602 if (mAnimationState) {
1603 IntRect rect = mAnimationState->UpdateState(this, mSize.ToUnknownSize());
1605 invalidRect.UnionRect(invalidRect,
1606 OrientedIntRect::FromUnknownRect(rect));
1610 // Tell the observers what happened.
1611 image->mProgressTracker->SyncNotifyProgress(aProgress,
1612 invalidRect.ToUnknownRect());
1615 void RasterImage::NotifyDecodeComplete(
1616 const DecoderFinalStatus& aStatus, const ImageMetadata& aMetadata,
1617 const DecoderTelemetry& aTelemetry, Progress aProgress,
1618 const OrientedIntRect& aInvalidRect, const Maybe<uint32_t>& aFrameCount,
1619 DecoderFlags aDecoderFlags, SurfaceFlags aSurfaceFlags) {
1620 MOZ_ASSERT(NS_IsMainThread());
1622 // If the decoder detected an error, log it to the error console.
1623 if (aStatus.mShouldReportError) {
1624 ReportDecoderError();
1627 // Record all the metadata the decoder gathered about this image.
1628 bool metadataOK = SetMetadata(aMetadata, aStatus.mWasMetadataDecode);
1629 if (!metadataOK) {
1630 // This indicates a serious error that requires us to discard all existing
1631 // surfaces and redecode to recover. We'll drop the results from this
1632 // decoder on the floor, since they aren't valid.
1633 RecoverFromInvalidFrames(mSize, FromSurfaceFlags(aSurfaceFlags));
1634 return;
1637 MOZ_ASSERT(mError || LoadHasSize() || !aMetadata.HasSize(),
1638 "SetMetadata should've gotten a size");
1640 if (!aStatus.mWasMetadataDecode && aStatus.mFinished) {
1641 // Flag that we've been decoded before.
1642 StoreHasBeenDecoded(true);
1645 // Send out any final notifications.
1646 NotifyProgress(aProgress, aInvalidRect, aFrameCount, aDecoderFlags,
1647 aSurfaceFlags);
1649 if (!(aDecoderFlags & DecoderFlags::FIRST_FRAME_ONLY)) {
1650 // We may have decoded new animation frames; update our animation state.
1651 MOZ_ASSERT_IF(aFrameCount && *aFrameCount > 1, mAnimationState || mError);
1652 if (mAnimationState && aFrameCount) {
1653 mAnimationState->UpdateKnownFrameCount(*aFrameCount);
1656 // If we should start animating right now, do so.
1657 if (mAnimationState && aFrameCount == Some(1u) && LoadPendingAnimation() &&
1658 ShouldAnimate()) {
1659 StartAnimation();
1662 if (mAnimationState && LoadHasBeenDecoded()) {
1663 // We've finished a full decode of all animation frames and our
1664 // AnimationState has been notified about them all, so let it know not to
1665 // expect anymore.
1666 mAnimationState->NotifyDecodeComplete();
1668 IntRect rect = mAnimationState->UpdateState(this, mSize.ToUnknownSize());
1670 if (!rect.IsEmpty()) {
1671 auto dirtyRect = OrientedIntRect::FromUnknownRect(rect);
1672 NotifyProgress(NoProgress, dirtyRect);
1677 // Do some telemetry if this isn't a metadata decode.
1678 if (!aStatus.mWasMetadataDecode) {
1679 if (aTelemetry.mChunkCount) {
1680 Telemetry::Accumulate(Telemetry::IMAGE_DECODE_CHUNKS,
1681 aTelemetry.mChunkCount);
1684 if (aStatus.mFinished) {
1685 Telemetry::Accumulate(Telemetry::IMAGE_DECODE_TIME,
1686 int32_t(aTelemetry.mDecodeTime.ToMicroseconds()));
1688 if (aTelemetry.mSpeedHistogram && aTelemetry.mBytesDecoded) {
1689 Telemetry::Accumulate(*aTelemetry.mSpeedHistogram, aTelemetry.Speed());
1694 // Only act on errors if we have no usable frames from the decoder.
1695 if (aStatus.mHadError &&
1696 (!mAnimationState || mAnimationState->KnownFrameCount() == 0)) {
1697 DoError();
1698 } else if (aStatus.mWasMetadataDecode && !LoadHasSize()) {
1699 DoError();
1702 // XXX(aosmond): Can we get this far without mFinished == true?
1703 if (aStatus.mFinished && aStatus.mWasMetadataDecode) {
1704 // If we were waiting to fire the load event, go ahead and fire it now.
1705 if (mLoadProgress) {
1706 NotifyForLoadEvent(*mLoadProgress);
1707 mLoadProgress = Nothing();
1710 // If we were a metadata decode and a full decode was requested, do it.
1711 if (LoadWantFullDecode()) {
1712 StoreWantFullDecode(false);
1713 RequestDecodeForSizeInternal(mSize,
1714 DECODE_FLAGS_DEFAULT |
1715 FLAG_HIGH_QUALITY_SCALING |
1716 FLAG_AVOID_REDECODE_FOR_SIZE,
1717 FRAME_CURRENT);
1722 void RasterImage::ReportDecoderError() {
1723 nsCOMPtr<nsIConsoleService> consoleService =
1724 do_GetService(NS_CONSOLESERVICE_CONTRACTID);
1725 nsCOMPtr<nsIScriptError> errorObject =
1726 do_CreateInstance(NS_SCRIPTERROR_CONTRACTID);
1728 if (consoleService && errorObject) {
1729 nsAutoString msg(u"Image corrupt or truncated."_ns);
1730 nsAutoString src;
1731 if (GetURI()) {
1732 nsAutoCString uri;
1733 if (!GetSpecTruncatedTo1k(uri)) {
1734 msg += u" URI in this note truncated due to length."_ns;
1736 CopyUTF8toUTF16(uri, src);
1738 if (NS_SUCCEEDED(errorObject->InitWithWindowID(msg, src, u""_ns, 0, 0,
1739 nsIScriptError::errorFlag,
1740 "Image", InnerWindowID()))) {
1741 consoleService->LogMessage(errorObject);
1746 already_AddRefed<imgIContainer> RasterImage::Unwrap() {
1747 nsCOMPtr<imgIContainer> self(this);
1748 return self.forget();
1751 void RasterImage::PropagateUseCounters(dom::Document*) {
1752 // No use counters.
1755 IntSize RasterImage::OptimalImageSizeForDest(const gfxSize& aDest,
1756 uint32_t aWhichFrame,
1757 SamplingFilter aSamplingFilter,
1758 uint32_t aFlags) {
1759 MOZ_ASSERT(aDest.width >= 0 || ceil(aDest.width) <= INT32_MAX ||
1760 aDest.height >= 0 || ceil(aDest.height) <= INT32_MAX,
1761 "Unexpected destination size");
1763 if (mSize.IsEmpty() || aDest.IsEmpty()) {
1764 return IntSize(0, 0);
1767 auto dest = OrientedIntSize::FromUnknownSize(
1768 IntSize::Ceil(aDest.width, aDest.height));
1770 if (aSamplingFilter == SamplingFilter::GOOD &&
1771 CanDownscaleDuringDecode(dest, aFlags)) {
1772 return dest.ToUnknownSize();
1775 // We can't scale to this size. Use our intrinsic size for now.
1776 return mSize.ToUnknownSize();
1779 } // namespace image
1780 } // namespace mozilla