Bug 1646700 [wpt PR 24235] - Update picture-in-picture idlharness test, a=testonly
[gecko.git] / image / RasterImage.cpp
blobd2fff56600d53523b9d6b4b4945c83c8b2d04618
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"
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*>()),
81 mHasSize(false),
82 mTransient(false),
83 mSyncLoad(false),
84 mDiscardable(false),
85 mSomeSourceData(false),
86 mAllSourceData(false),
87 mHasBeenDecoded(false),
88 mPendingAnimation(false),
89 mAnimationFinished(false),
90 mWantFullDecode(false),
91 mHandledOrientation(StaticPrefs::image_honor_orientation_metadata()) {
94 //******************************************************************************
95 RasterImage::~RasterImage() {
96 // Make sure our SourceBuffer is marked as complete. This will ensure that any
97 // outstanding decoders terminate.
98 if (!mSourceBuffer->IsComplete()) {
99 mSourceBuffer->Complete(NS_ERROR_ABORT);
102 // Release all frames from the surface cache.
103 SurfaceCache::RemoveImage(ImageKey(this));
105 // Record Telemetry.
106 Telemetry::Accumulate(Telemetry::IMAGE_DECODE_COUNT, mDecodeCount);
109 nsresult RasterImage::Init(const char* aMimeType, uint32_t aFlags) {
110 // We don't support re-initialization
111 if (mInitialized) {
112 return NS_ERROR_ILLEGAL_VALUE;
115 // Not sure an error can happen before init, but be safe
116 if (mError) {
117 return NS_ERROR_FAILURE;
120 // We want to avoid redecodes for transient images.
121 MOZ_ASSERT_IF(aFlags & INIT_FLAG_TRANSIENT,
122 !(aFlags & INIT_FLAG_DISCARDABLE));
124 // Store initialization data
125 mDiscardable = !!(aFlags & INIT_FLAG_DISCARDABLE);
126 mWantFullDecode = !!(aFlags & INIT_FLAG_DECODE_IMMEDIATELY);
127 mTransient = !!(aFlags & INIT_FLAG_TRANSIENT);
128 mSyncLoad = !!(aFlags & INIT_FLAG_SYNC_LOAD);
130 // Use the MIME type to select a decoder type, and make sure there *is* a
131 // decoder for this MIME type.
132 NS_ENSURE_ARG_POINTER(aMimeType);
133 mDecoderType = DecoderFactory::GetDecoderType(aMimeType);
134 if (mDecoderType == DecoderType::UNKNOWN) {
135 return NS_ERROR_FAILURE;
138 // Lock this image's surfaces in the SurfaceCache if we're not discardable.
139 if (!mDiscardable) {
140 mLockCount++;
141 SurfaceCache::LockImage(ImageKey(this));
144 // Mark us as initialized
145 mInitialized = true;
147 return NS_OK;
150 //******************************************************************************
151 NS_IMETHODIMP_(void)
152 RasterImage::RequestRefresh(const TimeStamp& aTime) {
153 if (HadRecentRefresh(aTime)) {
154 return;
157 EvaluateAnimation();
159 if (!mAnimating) {
160 return;
163 RefreshResult res;
164 if (mAnimationState) {
165 MOZ_ASSERT(mFrameAnimator);
166 res = mFrameAnimator->RequestRefresh(*mAnimationState, aTime);
169 #ifdef DEBUG
170 if (res.mFrameAdvanced) {
171 mFramesNotified++;
173 #endif
175 // Notify listeners that our frame has actually changed, but do this only
176 // once for all frames that we've now passed (if AdvanceFrame() was called
177 // more than once).
178 if (!res.mDirtyRect.IsEmpty() || res.mFrameAdvanced) {
179 auto dirtyRect = UnorientedIntRect::FromUnknownRect(res.mDirtyRect);
180 NotifyProgress(NoProgress, dirtyRect);
183 if (res.mAnimationFinished) {
184 mAnimationFinished = true;
185 EvaluateAnimation();
189 //******************************************************************************
190 NS_IMETHODIMP
191 RasterImage::GetWidth(int32_t* aWidth) {
192 NS_ENSURE_ARG_POINTER(aWidth);
194 if (mError) {
195 *aWidth = 0;
196 return NS_ERROR_FAILURE;
199 *aWidth = mSize.width;
200 return NS_OK;
203 //******************************************************************************
204 NS_IMETHODIMP
205 RasterImage::GetHeight(int32_t* aHeight) {
206 NS_ENSURE_ARG_POINTER(aHeight);
208 if (mError) {
209 *aHeight = 0;
210 return NS_ERROR_FAILURE;
213 *aHeight = mSize.height;
214 return NS_OK;
217 //******************************************************************************
218 nsresult RasterImage::GetNativeSizes(nsTArray<IntSize>& aNativeSizes) const {
219 if (mError) {
220 return NS_ERROR_FAILURE;
223 aNativeSizes.Clear();
225 if (mNativeSizes.IsEmpty()) {
226 aNativeSizes.AppendElement(mSize.ToUnknownSize());
227 } else {
228 for (const auto& size : mNativeSizes) {
229 aNativeSizes.AppendElement(size.ToUnknownSize());
233 return NS_OK;
236 //******************************************************************************
237 size_t RasterImage::GetNativeSizesLength() const {
238 if (mError || !mHasSize) {
239 return 0;
242 if (mNativeSizes.IsEmpty()) {
243 return 1;
246 return mNativeSizes.Length();
249 //******************************************************************************
250 NS_IMETHODIMP
251 RasterImage::GetIntrinsicSize(nsSize* aSize) {
252 if (mError) {
253 return NS_ERROR_FAILURE;
256 *aSize = nsSize(nsPresContext::CSSPixelsToAppUnits(mSize.width),
257 nsPresContext::CSSPixelsToAppUnits(mSize.height));
258 return NS_OK;
261 //******************************************************************************
262 Maybe<AspectRatio> RasterImage::GetIntrinsicRatio() {
263 if (mError) {
264 return Nothing();
267 return Some(AspectRatio::FromSize(mSize.width, mSize.height));
270 NS_IMETHODIMP_(Orientation)
271 RasterImage::GetOrientation() { return mOrientation; }
273 NS_IMETHODIMP_(bool)
274 RasterImage::HandledOrientation() { return mHandledOrientation; }
276 //******************************************************************************
277 NS_IMETHODIMP
278 RasterImage::GetType(uint16_t* aType) {
279 NS_ENSURE_ARG_POINTER(aType);
281 *aType = imgIContainer::TYPE_RASTER;
282 return NS_OK;
285 NS_IMETHODIMP
286 RasterImage::GetProducerId(uint32_t* aId) {
287 NS_ENSURE_ARG_POINTER(aId);
289 *aId = ImageResource::GetImageProducerId();
290 return NS_OK;
293 LookupResult RasterImage::LookupFrameInternal(const UnorientedIntSize& aSize,
294 uint32_t aFlags,
295 PlaybackType aPlaybackType,
296 bool aMarkUsed) {
297 if (mAnimationState && aPlaybackType == PlaybackType::eAnimated) {
298 MOZ_ASSERT(mFrameAnimator);
299 MOZ_ASSERT(ToSurfaceFlags(aFlags) == DefaultSurfaceFlags(),
300 "Can't composite frames with non-default surface flags");
301 return mFrameAnimator->GetCompositedFrame(*mAnimationState, aMarkUsed);
304 SurfaceFlags surfaceFlags = ToSurfaceFlags(aFlags);
306 // We don't want any substitution for sync decodes, and substitution would be
307 // illegal when high quality downscaling is disabled, so we use
308 // SurfaceCache::Lookup in this case.
309 if ((aFlags & FLAG_SYNC_DECODE) || !(aFlags & FLAG_HIGH_QUALITY_SCALING)) {
310 return SurfaceCache::Lookup(
311 ImageKey(this),
312 RasterSurfaceKey(aSize.ToUnknownSize(), surfaceFlags,
313 PlaybackType::eStatic),
314 aMarkUsed);
317 // We'll return the best match we can find to the requested frame.
318 return SurfaceCache::LookupBestMatch(
319 ImageKey(this),
320 RasterSurfaceKey(aSize.ToUnknownSize(), surfaceFlags,
321 PlaybackType::eStatic),
322 aMarkUsed);
325 LookupResult RasterImage::LookupFrame(const UnorientedIntSize& aSize,
326 uint32_t aFlags,
327 PlaybackType aPlaybackType,
328 bool aMarkUsed) {
329 MOZ_ASSERT(NS_IsMainThread());
331 // If we're opaque, we don't need to care about premultiplied alpha, because
332 // that can only matter for frames with transparency.
333 if (IsOpaque()) {
334 aFlags &= ~FLAG_DECODE_NO_PREMULTIPLY_ALPHA;
337 UnorientedIntSize requestedSize =
338 CanDownscaleDuringDecode(aSize, aFlags) ? aSize : ToUnoriented(mSize);
339 if (requestedSize.IsEmpty()) {
340 // Can't decode to a surface of zero size.
341 return LookupResult(MatchType::NOT_FOUND);
344 LookupResult result =
345 LookupFrameInternal(requestedSize, aFlags, aPlaybackType, aMarkUsed);
347 if (!result && !mHasSize) {
348 // We can't request a decode without knowing our intrinsic size. Give up.
349 return LookupResult(MatchType::NOT_FOUND);
352 const bool syncDecode = aFlags & FLAG_SYNC_DECODE;
353 const bool avoidRedecode = aFlags & FLAG_AVOID_REDECODE_FOR_SIZE;
354 if (result.Type() == MatchType::NOT_FOUND ||
355 (result.Type() == MatchType::SUBSTITUTE_BECAUSE_NOT_FOUND &&
356 !avoidRedecode) ||
357 (syncDecode && !avoidRedecode && !result)) {
358 // We don't have a copy of this frame, and there's no decoder working on
359 // one. (Or we're sync decoding and the existing decoder hasn't even started
360 // yet.) Trigger decoding so it'll be available next time.
361 MOZ_ASSERT(aPlaybackType != PlaybackType::eAnimated ||
362 StaticPrefs::image_mem_animated_discardable_AtStartup() ||
363 !mAnimationState || mAnimationState->KnownFrameCount() < 1,
364 "Animated frames should be locked");
366 // The surface cache may suggest the preferred size we are supposed to
367 // decode at. This should only happen if we accept substitutions.
368 if (!result.SuggestedSize().IsEmpty()) {
369 MOZ_ASSERT(!syncDecode && (aFlags & FLAG_HIGH_QUALITY_SCALING));
370 requestedSize =
371 UnorientedIntSize::FromUnknownSize(result.SuggestedSize());
374 bool ranSync = false, failed = false;
375 Decode(requestedSize, aFlags, aPlaybackType, ranSync, failed);
376 if (failed) {
377 result.SetFailedToRequestDecode();
380 // If we can or did sync decode, we should already have the frame.
381 if (ranSync || syncDecode) {
382 result =
383 LookupFrameInternal(requestedSize, aFlags, aPlaybackType, aMarkUsed);
387 if (!result) {
388 // We still weren't able to get a frame. Give up.
389 return result;
392 // Sync decoding guarantees that we got the frame, but if it's owned by an
393 // async decoder that's currently running, the contents of the frame may not
394 // be available yet. Make sure we get everything.
395 if (mAllSourceData && syncDecode) {
396 result.Surface()->WaitUntilFinished();
399 // If we could have done some decoding in this function we need to check if
400 // that decoding encountered an error and hence aborted the surface. We want
401 // to avoid calling IsAborted if we weren't passed any sync decode flag
402 // because IsAborted acquires the monitor for the imgFrame.
403 if (aFlags & (FLAG_SYNC_DECODE | FLAG_SYNC_DECODE_IF_FAST) &&
404 result.Surface()->IsAborted()) {
405 DrawableSurface tmp = std::move(result.Surface());
406 return result;
409 return result;
412 bool RasterImage::IsOpaque() {
413 if (mError) {
414 return false;
417 Progress progress = mProgressTracker->GetProgress();
419 // If we haven't yet finished decoding, the safe answer is "not opaque".
420 if (!(progress & FLAG_DECODE_COMPLETE)) {
421 return false;
424 // Other, we're opaque if FLAG_HAS_TRANSPARENCY is not set.
425 return !(progress & FLAG_HAS_TRANSPARENCY);
428 NS_IMETHODIMP_(bool)
429 RasterImage::WillDrawOpaqueNow() {
430 if (!IsOpaque()) {
431 return false;
434 if (mAnimationState) {
435 if (!StaticPrefs::image_mem_animated_discardable_AtStartup()) {
436 // We never discard frames of animated images.
437 return true;
438 } else {
439 if (mAnimationState->GetCompositedFrameInvalid()) {
440 // We're not going to draw anything at all.
441 return false;
446 // If we are not locked our decoded data could get discard at any time (ie
447 // between the call to this function and when we are asked to draw), so we
448 // have to return false if we are unlocked.
449 if (mLockCount == 0) {
450 return false;
453 auto size = ToUnoriented(mSize);
454 LookupResult result = SurfaceCache::LookupBestMatch(
455 ImageKey(this),
456 RasterSurfaceKey(size.ToUnknownSize(), DefaultSurfaceFlags(),
457 PlaybackType::eStatic),
458 /* aMarkUsed = */ false);
459 MatchType matchType = result.Type();
460 if (matchType == MatchType::NOT_FOUND || matchType == MatchType::PENDING ||
461 !result.Surface()->IsFinished()) {
462 return false;
465 return true;
468 void RasterImage::OnSurfaceDiscarded(const SurfaceKey& aSurfaceKey) {
469 MOZ_ASSERT(mProgressTracker);
471 bool animatedFramesDiscarded =
472 mAnimationState && aSurfaceKey.Playback() == PlaybackType::eAnimated;
474 nsCOMPtr<nsIEventTarget> eventTarget;
475 if (mProgressTracker) {
476 eventTarget = mProgressTracker->GetEventTarget();
477 } else {
478 eventTarget = do_GetMainThread();
481 RefPtr<RasterImage> image = this;
482 nsCOMPtr<nsIRunnable> ev =
483 NS_NewRunnableFunction("RasterImage::OnSurfaceDiscarded", [=]() -> void {
484 image->OnSurfaceDiscardedInternal(animatedFramesDiscarded);
486 eventTarget->Dispatch(ev.forget(), NS_DISPATCH_NORMAL);
489 void RasterImage::OnSurfaceDiscardedInternal(bool aAnimatedFramesDiscarded) {
490 MOZ_ASSERT(NS_IsMainThread());
492 if (aAnimatedFramesDiscarded && mAnimationState) {
493 MOZ_ASSERT(StaticPrefs::image_mem_animated_discardable_AtStartup());
494 ReleaseImageContainer();
496 auto size = ToUnoriented(mSize);
497 IntRect rect = mAnimationState->UpdateState(this, size.ToUnknownSize());
499 auto dirtyRect = UnorientedIntRect::FromUnknownRect(rect);
500 NotifyProgress(NoProgress, dirtyRect);
503 if (mProgressTracker) {
504 mProgressTracker->OnDiscard();
508 //******************************************************************************
509 NS_IMETHODIMP
510 RasterImage::GetAnimated(bool* aAnimated) {
511 if (mError) {
512 return NS_ERROR_FAILURE;
515 NS_ENSURE_ARG_POINTER(aAnimated);
517 // If we have an AnimationState, we can know for sure.
518 if (mAnimationState) {
519 *aAnimated = true;
520 return NS_OK;
523 // Otherwise, we need to have been decoded to know for sure, since if we were
524 // decoded at least once mAnimationState would have been created for animated
525 // images. This is true even though we check for animation during the
526 // metadata decode, because we may still discover animation only during the
527 // full decode for corrupt images.
528 if (!mHasBeenDecoded) {
529 return NS_ERROR_NOT_AVAILABLE;
532 // We know for sure
533 *aAnimated = false;
535 return NS_OK;
538 //******************************************************************************
539 NS_IMETHODIMP_(int32_t)
540 RasterImage::GetFirstFrameDelay() {
541 if (mError) {
542 return -1;
545 bool animated = false;
546 if (NS_FAILED(GetAnimated(&animated)) || !animated) {
547 return -1;
550 MOZ_ASSERT(mAnimationState, "Animated images should have an AnimationState");
551 return mAnimationState->FirstFrameTimeout().AsEncodedValueDeprecated();
554 NS_IMETHODIMP_(already_AddRefed<SourceSurface>)
555 RasterImage::GetFrame(uint32_t aWhichFrame, uint32_t aFlags) {
556 return GetFrameAtSize(mSize.ToUnknownSize(), aWhichFrame, aFlags);
559 NS_IMETHODIMP_(already_AddRefed<SourceSurface>)
560 RasterImage::GetFrameAtSize(const IntSize& aSize, uint32_t aWhichFrame,
561 uint32_t aFlags) {
562 #ifdef DEBUG
563 NotifyDrawingObservers();
564 #endif
566 auto result = GetFrameInternal(aSize, Nothing(), aWhichFrame, aFlags);
567 return mozilla::Get<2>(result).forget();
570 Tuple<ImgDrawResult, IntSize, RefPtr<SourceSurface>>
571 RasterImage::GetFrameInternal(const IntSize& aSize,
572 const Maybe<SVGImageContext>& aSVGContext,
573 uint32_t aWhichFrame, uint32_t aFlags) {
574 MOZ_ASSERT(aWhichFrame <= FRAME_MAX_VALUE);
576 auto size = OrientedIntSize::FromUnknownSize(aSize);
578 if (aSize.IsEmpty() || aWhichFrame > FRAME_MAX_VALUE) {
579 return MakeTuple(ImgDrawResult::BAD_ARGS, aSize, RefPtr<SourceSurface>());
582 if (mError) {
583 return MakeTuple(ImgDrawResult::BAD_IMAGE, aSize, RefPtr<SourceSurface>());
586 // Get the frame. If it's not there, it's probably the caller's fault for
587 // not waiting for the data to be loaded from the network or not passing
588 // FLAG_SYNC_DECODE.
589 LookupResult result =
590 LookupFrame(ToUnoriented(size), aFlags, ToPlaybackType(aWhichFrame),
591 /* aMarkUsed = */ true);
592 auto resultSuggestedSize =
593 UnorientedIntSize::FromUnknownSize(result.SuggestedSize());
595 // The surface cache may have suggested we use a different size than the
596 // given size in the future. This may or may not be accompanied by an
597 // actual surface, depending on what it has in its cache.
598 OrientedIntSize suggestedSize = ToOriented(resultSuggestedSize);
599 if (suggestedSize.IsEmpty()) {
600 suggestedSize = size;
602 MOZ_ASSERT_IF(result.Type() == MatchType::SUBSTITUTE_BECAUSE_BEST,
603 suggestedSize != size);
605 if (!result) {
606 // The OS threw this frame away and we couldn't redecode it.
607 return MakeTuple(ImgDrawResult::TEMPORARY_ERROR,
608 suggestedSize.ToUnknownSize(), RefPtr<SourceSurface>());
611 RefPtr<SourceSurface> surface = result.Surface()->GetSourceSurface();
613 // If this RasterImage requires orientation, we must return a newly created
614 // surface with the oriented image instead of returning the frame's surface
615 // directly.
616 surface = OrientedImage::OrientSurface(UsedOrientation(), surface);
618 if (!result.Surface()->IsFinished()) {
619 return MakeTuple(ImgDrawResult::INCOMPLETE, suggestedSize.ToUnknownSize(),
620 std::move(surface));
623 return MakeTuple(ImgDrawResult::SUCCESS, suggestedSize.ToUnknownSize(),
624 std::move(surface));
627 Tuple<ImgDrawResult, IntSize> RasterImage::GetImageContainerSize(
628 LayerManager* aManager, const IntSize& aRequestedSize, uint32_t aFlags) {
629 if (!mHasSize) {
630 return MakeTuple(ImgDrawResult::NOT_READY, IntSize(0, 0));
633 if (aRequestedSize.IsEmpty()) {
634 return MakeTuple(ImgDrawResult::BAD_ARGS, IntSize(0, 0));
637 // We check the minimum size because while we support downscaling, we do not
638 // support upscaling. If aRequestedSize > mSize, we will never give a larger
639 // surface than mSize. If mSize > aRequestedSize, and mSize > maxTextureSize,
640 // we still want to use image containers if aRequestedSize <= maxTextureSize.
641 int32_t maxTextureSize = aManager->GetMaxTextureSize();
642 if (min(mSize.width, aRequestedSize.width) > maxTextureSize ||
643 min(mSize.height, aRequestedSize.height) > maxTextureSize) {
644 return MakeTuple(ImgDrawResult::NOT_SUPPORTED, IntSize(0, 0));
647 auto requestedSize = OrientedIntSize::FromUnknownSize(aRequestedSize);
648 if (!CanDownscaleDuringDecode(ToUnoriented(requestedSize), aFlags)) {
649 return MakeTuple(ImgDrawResult::SUCCESS, mSize.ToUnknownSize());
652 return MakeTuple(ImgDrawResult::SUCCESS, aRequestedSize);
655 NS_IMETHODIMP_(bool)
656 RasterImage::IsImageContainerAvailable(LayerManager* aManager,
657 uint32_t aFlags) {
658 return IsImageContainerAvailableAtSize(aManager, mSize.ToUnknownSize(),
659 aFlags);
662 NS_IMETHODIMP_(already_AddRefed<ImageContainer>)
663 RasterImage::GetImageContainer(LayerManager* aManager, uint32_t aFlags) {
664 RefPtr<ImageContainer> container;
665 ImgDrawResult drawResult =
666 GetImageContainerImpl(aManager, mSize.ToUnknownSize(), Nothing(), aFlags,
667 getter_AddRefs(container));
669 // We silence the unused warning here because anything that needs the draw
670 // result should be using GetImageContainerAtSize, not GetImageContainer.
671 (void)drawResult;
672 return container.forget();
675 NS_IMETHODIMP_(bool)
676 RasterImage::IsImageContainerAvailableAtSize(LayerManager* aManager,
677 const IntSize& aRequestedSize,
678 uint32_t aFlags) {
679 // We check the minimum size because while we support downscaling, we do not
680 // support upscaling. If aRequestedSize > mSize, we will never give a larger
681 // surface than mSize. If mSize > aRequestedSize, and mSize > maxTextureSize,
682 // we still want to use image containers if aRequestedSize <= maxTextureSize.
683 int32_t maxTextureSize = aManager->GetMaxTextureSize();
684 if (!mHasSize || aRequestedSize.IsEmpty() ||
685 min(mSize.width, aRequestedSize.width) > maxTextureSize ||
686 min(mSize.height, aRequestedSize.height) > maxTextureSize) {
687 return false;
690 return true;
693 NS_IMETHODIMP_(ImgDrawResult)
694 RasterImage::GetImageContainerAtSize(layers::LayerManager* aManager,
695 const gfx::IntSize& aSize,
696 const Maybe<SVGImageContext>& aSVGContext,
697 uint32_t aFlags,
698 layers::ImageContainer** aOutContainer) {
699 // We do not pass in the given SVG context because in theory it could differ
700 // between calls, but actually have no impact on the actual contents of the
701 // image container.
702 return GetImageContainerImpl(aManager, aSize, Nothing(), aFlags,
703 aOutContainer);
706 size_t RasterImage::SizeOfSourceWithComputedFallback(
707 SizeOfState& aState) const {
708 return mSourceBuffer->SizeOfIncludingThisWithComputedFallback(
709 aState.mMallocSizeOf);
712 void RasterImage::CollectSizeOfSurfaces(
713 nsTArray<SurfaceMemoryCounter>& aCounters,
714 MallocSizeOf aMallocSizeOf) const {
715 SurfaceCache::CollectSizeOfSurfaces(ImageKey(this), aCounters, aMallocSizeOf);
718 bool RasterImage::SetMetadata(const ImageMetadata& aMetadata,
719 bool aFromMetadataDecode) {
720 MOZ_ASSERT(NS_IsMainThread());
722 if (mError) {
723 return true;
726 if (aMetadata.HasSize()) {
727 auto metadataSize = UnorientedIntSize::FromUnknownSize(aMetadata.GetSize());
728 if (metadataSize.width < 0 || metadataSize.height < 0) {
729 NS_WARNING("Image has negative intrinsic size");
730 DoError();
731 return true;
734 MOZ_ASSERT(aMetadata.HasOrientation());
735 Orientation orientation = aMetadata.GetOrientation();
737 // If we already have a size, check the new size against the old one.
738 if (mHasSize &&
739 (metadataSize != ToUnoriented(mSize) || orientation != mOrientation)) {
740 NS_WARNING(
741 "Image changed size or orientation on redecode! "
742 "This should not happen!");
743 DoError();
744 return true;
747 // Set the size and flag that we have it.
748 mOrientation = orientation;
749 mSize = ToOriented(metadataSize);
750 mNativeSizes.Clear();
751 for (const auto& nativeSize : aMetadata.GetNativeSizes()) {
752 mNativeSizes.AppendElement(
753 ToOriented(UnorientedIntSize::FromUnknownSize(nativeSize)));
755 mHasSize = true;
758 if (mHasSize && aMetadata.HasAnimation() && !mAnimationState) {
759 // We're becoming animated, so initialize animation stuff.
760 mAnimationState.emplace(mAnimationMode);
761 mFrameAnimator =
762 MakeUnique<FrameAnimator>(this, ToUnoriented(mSize).ToUnknownSize());
764 if (!StaticPrefs::image_mem_animated_discardable_AtStartup()) {
765 // We don't support discarding animated images (See bug 414259).
766 // Lock the image and throw away the key.
767 LockImage();
770 if (!aFromMetadataDecode) {
771 // The metadata decode reported that this image isn't animated, but we
772 // discovered that it actually was during the full decode. This is a
773 // rare failure that only occurs for corrupt images. To recover, we need
774 // to discard all existing surfaces and redecode.
775 return false;
779 if (mAnimationState) {
780 mAnimationState->SetLoopCount(aMetadata.GetLoopCount());
781 mAnimationState->SetFirstFrameTimeout(aMetadata.GetFirstFrameTimeout());
783 if (aMetadata.HasLoopLength()) {
784 mAnimationState->SetLoopLength(aMetadata.GetLoopLength());
786 if (aMetadata.HasFirstFrameRefreshArea()) {
787 mAnimationState->SetFirstFrameRefreshArea(
788 aMetadata.GetFirstFrameRefreshArea());
792 if (aMetadata.HasHotspot()) {
793 // NOTE(heycam): We shouldn't have any image formats that support both
794 // orientation and hotspots, so we assert that rather than add code
795 // to orient the hotspot point correctly.
796 MOZ_ASSERT(UsedOrientation().IsIdentity(),
797 "Would need to orient hotspot point");
799 auto hotspot = aMetadata.GetHotspot();
800 mHotspot.x = std::max(std::min(hotspot.x, mSize.width - 1), 0);
801 mHotspot.y = std::max(std::min(hotspot.y, mSize.height - 1), 0);
804 return true;
807 NS_IMETHODIMP
808 RasterImage::SetAnimationMode(uint16_t aAnimationMode) {
809 if (mAnimationState) {
810 mAnimationState->SetAnimationMode(aAnimationMode);
812 return SetAnimationModeInternal(aAnimationMode);
815 //******************************************************************************
817 nsresult RasterImage::StartAnimation() {
818 if (mError) {
819 return NS_ERROR_FAILURE;
822 MOZ_ASSERT(ShouldAnimate(), "Should not animate!");
824 // If we're not ready to animate, then set mPendingAnimation, which will cause
825 // us to start animating if and when we do become ready.
826 mPendingAnimation =
827 !mAnimationState || mAnimationState->KnownFrameCount() < 1;
828 if (mPendingAnimation) {
829 return NS_OK;
832 // Don't bother to animate if we're displaying the first frame forever.
833 if (mAnimationState->GetCurrentAnimationFrameIndex() == 0 &&
834 mAnimationState->FirstFrameTimeout() == FrameTimeout::Forever()) {
835 mAnimationFinished = true;
836 return NS_ERROR_ABORT;
839 // We need to set the time that this initial frame was first displayed, as
840 // this is used in AdvanceFrame().
841 mAnimationState->InitAnimationFrameTimeIfNecessary();
843 return NS_OK;
846 //******************************************************************************
847 nsresult RasterImage::StopAnimation() {
848 MOZ_ASSERT(mAnimating, "Should be animating!");
850 nsresult rv = NS_OK;
851 if (mError) {
852 rv = NS_ERROR_FAILURE;
853 } else {
854 mAnimationState->SetAnimationFrameTime(TimeStamp());
857 mAnimating = false;
858 return rv;
861 //******************************************************************************
862 NS_IMETHODIMP
863 RasterImage::ResetAnimation() {
864 if (mError) {
865 return NS_ERROR_FAILURE;
868 mPendingAnimation = false;
870 if (mAnimationMode == kDontAnimMode || !mAnimationState ||
871 mAnimationState->GetCurrentAnimationFrameIndex() == 0) {
872 return NS_OK;
875 mAnimationFinished = false;
877 if (mAnimating) {
878 StopAnimation();
881 MOZ_ASSERT(mAnimationState, "Should have AnimationState");
882 MOZ_ASSERT(mFrameAnimator, "Should have FrameAnimator");
883 mFrameAnimator->ResetAnimation(*mAnimationState);
885 IntRect area = mAnimationState->FirstFrameRefreshArea();
886 NotifyProgress(NoProgress, UnorientedIntRect::FromUnknownRect(area));
888 // Start the animation again. It may not have been running before, if
889 // mAnimationFinished was true before entering this function.
890 EvaluateAnimation();
892 return NS_OK;
895 //******************************************************************************
896 NS_IMETHODIMP_(void)
897 RasterImage::SetAnimationStartTime(const TimeStamp& aTime) {
898 if (mError || mAnimationMode == kDontAnimMode || mAnimating ||
899 !mAnimationState) {
900 return;
903 mAnimationState->SetAnimationFrameTime(aTime);
906 NS_IMETHODIMP_(float)
907 RasterImage::GetFrameIndex(uint32_t aWhichFrame) {
908 MOZ_ASSERT(aWhichFrame <= FRAME_MAX_VALUE, "Invalid argument");
909 return (aWhichFrame == FRAME_FIRST || !mAnimationState)
910 ? 0.0f
911 : mAnimationState->GetCurrentAnimationFrameIndex();
914 NS_IMETHODIMP_(IntRect)
915 RasterImage::GetImageSpaceInvalidationRect(const IntRect& aRect) {
916 // Note that we do not transform aRect into an UnorientedIntRect, since
917 // RasterImage::NotifyProgress notifies all consumers of the image using
918 // OrientedIntRect values. (This is unlike OrientedImage, which notifies
919 // using inner image coordinates.)
920 return aRect;
923 nsresult RasterImage::OnImageDataComplete(nsIRequest*, nsISupports*,
924 nsresult aStatus, bool aLastPart) {
925 MOZ_ASSERT(NS_IsMainThread());
927 // Record that we have all the data we're going to get now.
928 mAllSourceData = true;
930 // Let decoders know that there won't be any more data coming.
931 mSourceBuffer->Complete(aStatus);
933 // Allow a synchronous metadata decode if mSyncLoad was set, or if we're
934 // running on a single thread (in which case waiting for the async metadata
935 // decoder could delay this image's load event quite a bit), or if this image
936 // is transient.
937 bool canSyncDecodeMetadata =
938 mSyncLoad || mTransient || DecodePool::NumberOfCores() < 2;
940 if (canSyncDecodeMetadata && !mHasSize) {
941 // We're loading this image synchronously, so it needs to be usable after
942 // this call returns. Since we haven't gotten our size yet, we need to do a
943 // synchronous metadata decode here.
944 DecodeMetadata(FLAG_SYNC_DECODE);
947 // Determine our final status, giving precedence to Necko failure codes. We
948 // check after running the metadata decode in case it triggered an error.
949 nsresult finalStatus = mError ? NS_ERROR_FAILURE : NS_OK;
950 if (NS_FAILED(aStatus)) {
951 finalStatus = aStatus;
954 // If loading failed, report an error.
955 if (NS_FAILED(finalStatus)) {
956 DoError();
959 Progress loadProgress = LoadCompleteProgress(aLastPart, mError, finalStatus);
961 if (!mHasSize && !mError) {
962 // We don't have our size yet, so we'll fire the load event in SetSize().
963 MOZ_ASSERT(!canSyncDecodeMetadata,
964 "Firing load async after metadata sync decode?");
965 mLoadProgress = Some(loadProgress);
966 return finalStatus;
969 NotifyForLoadEvent(loadProgress);
971 return finalStatus;
974 void RasterImage::NotifyForLoadEvent(Progress aProgress) {
975 MOZ_ASSERT(mHasSize || mError, "Need to know size before firing load event");
976 MOZ_ASSERT(
977 !mHasSize || (mProgressTracker->GetProgress() & FLAG_SIZE_AVAILABLE),
978 "Should have notified that the size is available if we have it");
980 // If we encountered an error, make sure we notify for that as well.
981 if (mError) {
982 aProgress |= FLAG_HAS_ERROR;
985 // Notify our listeners, which will fire this image's load event.
986 NotifyProgress(aProgress);
989 nsresult RasterImage::OnImageDataAvailable(nsIRequest*, nsISupports*,
990 nsIInputStream* aInputStream,
991 uint64_t, uint32_t aCount) {
992 nsresult rv = mSourceBuffer->AppendFromInputStream(aInputStream, aCount);
993 if (NS_SUCCEEDED(rv) && !mSomeSourceData) {
994 mSomeSourceData = true;
995 if (!mSyncLoad) {
996 // Create an async metadata decoder and verify we succeed in doing so.
997 rv = DecodeMetadata(DECODE_FLAGS_DEFAULT);
1001 if (NS_FAILED(rv)) {
1002 DoError();
1004 return rv;
1007 nsresult RasterImage::SetSourceSizeHint(uint32_t aSizeHint) {
1008 if (aSizeHint == 0) {
1009 return NS_OK;
1012 nsresult rv = mSourceBuffer->ExpectLength(aSizeHint);
1013 if (rv == NS_ERROR_OUT_OF_MEMORY) {
1014 // Flush memory, try to get some back, and try again.
1015 rv = nsMemory::HeapMinimize(true);
1016 if (NS_SUCCEEDED(rv)) {
1017 rv = mSourceBuffer->ExpectLength(aSizeHint);
1021 return rv;
1024 nsresult RasterImage::GetHotspotX(int32_t* aX) {
1025 *aX = mHotspot.x;
1026 return NS_OK;
1029 nsresult RasterImage::GetHotspotY(int32_t* aY) {
1030 *aY = mHotspot.y;
1031 return NS_OK;
1034 void RasterImage::Discard() {
1035 MOZ_ASSERT(NS_IsMainThread());
1036 MOZ_ASSERT(CanDiscard(), "Asked to discard but can't");
1037 MOZ_ASSERT(!mAnimationState ||
1038 StaticPrefs::image_mem_animated_discardable_AtStartup(),
1039 "Asked to discard for animated image");
1041 // Delete all the decoded frames.
1042 SurfaceCache::RemoveImage(ImageKey(this));
1044 if (mAnimationState) {
1045 ReleaseImageContainer();
1047 auto size = ToUnoriented(mSize);
1048 IntRect rect = mAnimationState->UpdateState(this, size.ToUnknownSize());
1050 auto dirtyRect = UnorientedIntRect::FromUnknownRect(rect);
1051 NotifyProgress(NoProgress, dirtyRect);
1054 // Notify that we discarded.
1055 if (mProgressTracker) {
1056 mProgressTracker->OnDiscard();
1060 bool RasterImage::CanDiscard() {
1061 return mAllSourceData &&
1062 // Can discard animated images if the pref is set
1063 (!mAnimationState ||
1064 StaticPrefs::image_mem_animated_discardable_AtStartup());
1067 NS_IMETHODIMP
1068 RasterImage::StartDecoding(uint32_t aFlags, uint32_t aWhichFrame) {
1069 if (mError) {
1070 return NS_ERROR_FAILURE;
1073 if (!mHasSize) {
1074 mWantFullDecode = true;
1075 return NS_OK;
1078 uint32_t flags = (aFlags & FLAG_ASYNC_NOTIFY) | FLAG_SYNC_DECODE_IF_FAST |
1079 FLAG_HIGH_QUALITY_SCALING;
1080 return RequestDecodeForSize(mSize.ToUnknownSize(), flags, aWhichFrame);
1083 bool RasterImage::StartDecodingWithResult(uint32_t aFlags,
1084 uint32_t aWhichFrame) {
1085 if (mError) {
1086 return false;
1089 if (!mHasSize) {
1090 mWantFullDecode = true;
1091 return false;
1094 uint32_t flags = (aFlags & FLAG_ASYNC_NOTIFY) | FLAG_SYNC_DECODE_IF_FAST |
1095 FLAG_HIGH_QUALITY_SCALING;
1096 LookupResult result =
1097 RequestDecodeForSizeInternal(ToUnoriented(mSize), flags, aWhichFrame);
1098 DrawableSurface surface = std::move(result.Surface());
1099 return surface && surface->IsFinished();
1102 imgIContainer::DecodeResult RasterImage::RequestDecodeWithResult(
1103 uint32_t aFlags, uint32_t aWhichFrame) {
1104 MOZ_ASSERT(NS_IsMainThread());
1106 if (mError) {
1107 return imgIContainer::DECODE_REQUEST_FAILED;
1110 uint32_t flags = aFlags | FLAG_ASYNC_NOTIFY;
1111 LookupResult result =
1112 RequestDecodeForSizeInternal(ToUnoriented(mSize), flags, aWhichFrame);
1113 DrawableSurface surface = std::move(result.Surface());
1114 if (surface && surface->IsFinished()) {
1115 return imgIContainer::DECODE_SURFACE_AVAILABLE;
1117 if (result.GetFailedToRequestDecode()) {
1118 return imgIContainer::DECODE_REQUEST_FAILED;
1120 return imgIContainer::DECODE_REQUESTED;
1123 NS_IMETHODIMP
1124 RasterImage::RequestDecodeForSize(const IntSize& aSize, uint32_t aFlags,
1125 uint32_t aWhichFrame) {
1126 MOZ_ASSERT(NS_IsMainThread());
1128 if (mError) {
1129 return NS_ERROR_FAILURE;
1132 RequestDecodeForSizeInternal(
1133 ToUnoriented(OrientedIntSize::FromUnknownSize(aSize)), aFlags,
1134 aWhichFrame);
1136 return NS_OK;
1139 LookupResult RasterImage::RequestDecodeForSizeInternal(
1140 const UnorientedIntSize& aSize, uint32_t aFlags, uint32_t aWhichFrame) {
1141 MOZ_ASSERT(NS_IsMainThread());
1143 if (aWhichFrame > FRAME_MAX_VALUE) {
1144 return LookupResult(MatchType::NOT_FOUND);
1147 if (mError) {
1148 LookupResult result = LookupResult(MatchType::NOT_FOUND);
1149 result.SetFailedToRequestDecode();
1150 return result;
1153 if (!mHasSize) {
1154 mWantFullDecode = true;
1155 return LookupResult(MatchType::NOT_FOUND);
1158 // Decide whether to sync decode images we can decode quickly. Here we are
1159 // explicitly trading off flashing for responsiveness in the case that we're
1160 // redecoding an image (see bug 845147).
1161 bool shouldSyncDecodeIfFast =
1162 !mHasBeenDecoded && (aFlags & FLAG_SYNC_DECODE_IF_FAST);
1164 uint32_t flags =
1165 shouldSyncDecodeIfFast ? aFlags : aFlags & ~FLAG_SYNC_DECODE_IF_FAST;
1167 // Perform a frame lookup, which will implicitly start decoding if needed.
1168 return LookupFrame(aSize, flags, ToPlaybackType(aWhichFrame),
1169 /* aMarkUsed = */ false);
1172 static bool LaunchDecodingTask(IDecodingTask* aTask, RasterImage* aImage,
1173 uint32_t aFlags, bool aHaveSourceData) {
1174 if (aHaveSourceData) {
1175 nsCString uri(aImage->GetURIString());
1177 // If we have all the data, we can sync decode if requested.
1178 if (aFlags & imgIContainer::FLAG_SYNC_DECODE) {
1179 DecodePool::Singleton()->SyncRunIfPossible(aTask, uri);
1180 return true;
1183 if (aFlags & imgIContainer::FLAG_SYNC_DECODE_IF_FAST) {
1184 return DecodePool::Singleton()->SyncRunIfPreferred(aTask, uri);
1188 // Perform an async decode. We also take this path if we don't have all the
1189 // source data yet, since sync decoding is impossible in that situation.
1190 DecodePool::Singleton()->AsyncRun(aTask);
1191 return false;
1194 void RasterImage::Decode(const UnorientedIntSize& aSize, uint32_t aFlags,
1195 PlaybackType aPlaybackType, bool& aOutRanSync,
1196 bool& aOutFailed) {
1197 MOZ_ASSERT(NS_IsMainThread());
1199 if (mError) {
1200 aOutFailed = true;
1201 return;
1204 // If we don't have a size yet, we can't do any other decoding.
1205 if (!mHasSize) {
1206 mWantFullDecode = true;
1207 return;
1210 // We're about to decode again, which may mean that some of the previous sizes
1211 // we've decoded at aren't useful anymore. We can allow them to expire from
1212 // the cache by unlocking them here. When the decode finishes, it will send an
1213 // invalidation that will cause all instances of this image to redraw. If this
1214 // image is locked, any surfaces that are still useful will become locked
1215 // again when LookupFrame touches them, and the remainder will eventually
1216 // expire.
1217 SurfaceCache::UnlockEntries(ImageKey(this));
1219 // Determine which flags we need to decode this image with.
1220 DecoderFlags decoderFlags = DefaultDecoderFlags();
1221 if (aFlags & FLAG_ASYNC_NOTIFY) {
1222 decoderFlags |= DecoderFlags::ASYNC_NOTIFY;
1224 if (mTransient) {
1225 decoderFlags |= DecoderFlags::IMAGE_IS_TRANSIENT;
1227 if (mHasBeenDecoded) {
1228 decoderFlags |= DecoderFlags::IS_REDECODE;
1230 if ((aFlags & FLAG_SYNC_DECODE) || !(aFlags & FLAG_HIGH_QUALITY_SCALING)) {
1231 // Used SurfaceCache::Lookup instead of SurfaceCache::LookupBestMatch. That
1232 // means the caller can handle a differently sized surface to be returned
1233 // at any point.
1234 decoderFlags |= DecoderFlags::CANNOT_SUBSTITUTE;
1237 SurfaceFlags surfaceFlags = ToSurfaceFlags(aFlags);
1238 if (IsOpaque()) {
1239 // If there's no transparency, it doesn't matter whether we premultiply
1240 // alpha or not.
1241 surfaceFlags &= ~SurfaceFlags::NO_PREMULTIPLY_ALPHA;
1244 // Create a decoder.
1245 RefPtr<IDecodingTask> task;
1246 nsresult rv;
1247 bool animated = mAnimationState && aPlaybackType == PlaybackType::eAnimated;
1248 if (animated) {
1249 size_t currentFrame = mAnimationState->GetCurrentAnimationFrameIndex();
1250 rv = DecoderFactory::CreateAnimationDecoder(
1251 mDecoderType, WrapNotNull(this), mSourceBuffer,
1252 ToUnoriented(mSize).ToUnknownSize(), decoderFlags, surfaceFlags,
1253 currentFrame, getter_AddRefs(task));
1254 } else {
1255 rv = DecoderFactory::CreateDecoder(
1256 mDecoderType, WrapNotNull(this), mSourceBuffer,
1257 ToUnoriented(mSize).ToUnknownSize(), aSize.ToUnknownSize(),
1258 decoderFlags, surfaceFlags, getter_AddRefs(task));
1261 if (rv == NS_ERROR_ALREADY_INITIALIZED) {
1262 // We raced with an already pending decoder, and it finished before we
1263 // managed to insert the new decoder. Pretend we did a sync call to make
1264 // the caller lookup in the surface cache again.
1265 MOZ_ASSERT(!task);
1266 aOutRanSync = true;
1267 return;
1270 if (animated) {
1271 // We pass false for aAllowInvalidation because we may be asked to use
1272 // async notifications. Any potential invalidation here will be sent when
1273 // RequestRefresh is called, or NotifyDecodeComplete.
1274 #ifdef DEBUG
1275 IntRect rect =
1276 #endif
1277 mAnimationState->UpdateState(this, ToUnoriented(mSize).ToUnknownSize(),
1278 false);
1279 MOZ_ASSERT(rect.IsEmpty());
1282 // Make sure DecoderFactory was able to create a decoder successfully.
1283 if (NS_FAILED(rv)) {
1284 MOZ_ASSERT(!task);
1285 aOutFailed = true;
1286 return;
1289 MOZ_ASSERT(task);
1290 mDecodeCount++;
1292 // We're ready to decode; start the decoder.
1293 aOutRanSync = LaunchDecodingTask(task, this, aFlags, mAllSourceData);
1296 NS_IMETHODIMP
1297 RasterImage::DecodeMetadata(uint32_t aFlags) {
1298 if (mError) {
1299 return NS_ERROR_FAILURE;
1302 MOZ_ASSERT(!mHasSize, "Should not do unnecessary metadata decodes");
1304 // Create a decoder.
1305 RefPtr<IDecodingTask> task = DecoderFactory::CreateMetadataDecoder(
1306 mDecoderType, WrapNotNull(this), mSourceBuffer);
1308 // Make sure DecoderFactory was able to create a decoder successfully.
1309 if (!task) {
1310 return NS_ERROR_FAILURE;
1313 // We're ready to decode; start the decoder.
1314 LaunchDecodingTask(task, this, aFlags, mAllSourceData);
1315 return NS_OK;
1318 void RasterImage::RecoverFromInvalidFrames(const UnorientedIntSize& aSize,
1319 uint32_t aFlags) {
1320 if (!mHasSize) {
1321 return;
1324 NS_WARNING("A RasterImage's frames became invalid. Attempting to recover...");
1326 // Discard all existing frames, since they're probably all now invalid.
1327 SurfaceCache::RemoveImage(ImageKey(this));
1329 // Relock the image if it's supposed to be locked.
1330 if (mLockCount > 0) {
1331 SurfaceCache::LockImage(ImageKey(this));
1334 bool unused1, unused2;
1336 // Animated images require some special handling, because we normally require
1337 // that they never be discarded.
1338 if (mAnimationState) {
1339 Decode(ToUnoriented(mSize), aFlags | FLAG_SYNC_DECODE,
1340 PlaybackType::eAnimated, unused1, unused2);
1341 ResetAnimation();
1342 return;
1345 // For non-animated images, it's fine to recover using an async decode.
1346 Decode(aSize, aFlags, PlaybackType::eStatic, unused1, unused2);
1349 static bool HaveSkia() {
1350 #ifdef MOZ_ENABLE_SKIA
1351 return true;
1352 #else
1353 return false;
1354 #endif
1357 bool RasterImage::CanDownscaleDuringDecode(const UnorientedIntSize& aSize,
1358 uint32_t aFlags) {
1359 // Check basic requirements: downscale-during-decode is enabled, Skia is
1360 // available, this image isn't transient, we have all the source data and know
1361 // our size, and the flags allow us to do it.
1362 if (!mHasSize || mTransient || !HaveSkia() ||
1363 !StaticPrefs::image_downscale_during_decode_enabled() ||
1364 !(aFlags & imgIContainer::FLAG_HIGH_QUALITY_SCALING)) {
1365 return false;
1368 // We don't downscale animated images during decode.
1369 if (mAnimationState) {
1370 return false;
1373 // Never upscale.
1374 UnorientedIntSize ourSize = ToUnoriented(mSize);
1375 if (aSize.width >= ourSize.width || aSize.height >= ourSize.height) {
1376 return false;
1379 // Zero or negative width or height is unacceptable.
1380 if (aSize.width < 1 || aSize.height < 1) {
1381 return false;
1384 // There's no point in scaling if we can't store the result.
1385 if (!SurfaceCache::CanHold(aSize.ToUnknownSize())) {
1386 return false;
1389 return true;
1392 ImgDrawResult RasterImage::DrawInternal(DrawableSurface&& aSurface,
1393 gfxContext* aContext,
1394 const UnorientedIntSize& aSize,
1395 const ImageRegion& aRegion,
1396 SamplingFilter aSamplingFilter,
1397 uint32_t aFlags, float aOpacity) {
1398 gfxContextMatrixAutoSaveRestore saveMatrix(aContext);
1399 ImageRegion region(aRegion);
1400 bool frameIsFinished = aSurface->IsFinished();
1402 #ifdef DEBUG
1403 NotifyDrawingObservers();
1404 #endif
1406 // By now we may have a frame with the requested size. If not, we need to
1407 // adjust the drawing parameters accordingly.
1408 IntSize finalSize = aSurface->GetSize();
1409 bool couldRedecodeForBetterFrame = false;
1410 if (finalSize != aSize.ToUnknownSize()) {
1411 gfx::Size scale(double(aSize.width) / finalSize.width,
1412 double(aSize.height) / finalSize.height);
1413 aContext->Multiply(gfxMatrix::Scaling(scale.width, scale.height));
1414 region.Scale(1.0 / scale.width, 1.0 / scale.height);
1416 couldRedecodeForBetterFrame = CanDownscaleDuringDecode(aSize, aFlags);
1419 if (!aSurface->Draw(aContext, region, aSamplingFilter, aFlags, aOpacity)) {
1420 RecoverFromInvalidFrames(aSize, aFlags);
1421 return ImgDrawResult::TEMPORARY_ERROR;
1423 if (!frameIsFinished) {
1424 return ImgDrawResult::INCOMPLETE;
1426 if (couldRedecodeForBetterFrame) {
1427 return ImgDrawResult::WRONG_SIZE;
1429 return ImgDrawResult::SUCCESS;
1432 //******************************************************************************
1433 NS_IMETHODIMP_(ImgDrawResult)
1434 RasterImage::Draw(gfxContext* aContext, const IntSize& aSize,
1435 const ImageRegion& aRegion, uint32_t aWhichFrame,
1436 SamplingFilter aSamplingFilter,
1437 const Maybe<SVGImageContext>& /*aSVGContext - ignored*/,
1438 uint32_t aFlags, float aOpacity) {
1439 if (aWhichFrame > FRAME_MAX_VALUE) {
1440 return ImgDrawResult::BAD_ARGS;
1443 if (mError) {
1444 return ImgDrawResult::BAD_IMAGE;
1447 // Illegal -- you can't draw with non-default decode flags.
1448 // (Disabling colorspace conversion might make sense to allow, but
1449 // we don't currently.)
1450 if (ToSurfaceFlags(aFlags) != DefaultSurfaceFlags()) {
1451 return ImgDrawResult::BAD_ARGS;
1454 if (!aContext) {
1455 return ImgDrawResult::BAD_ARGS;
1458 if (mAnimationConsumers == 0) {
1459 SendOnUnlockedDraw(aFlags);
1462 // If we're not using SamplingFilter::GOOD, we shouldn't high-quality scale or
1463 // downscale during decode.
1464 uint32_t flags = aSamplingFilter == SamplingFilter::GOOD
1465 ? aFlags
1466 : aFlags & ~FLAG_HIGH_QUALITY_SCALING;
1468 auto size = ToUnoriented(OrientedIntSize::FromUnknownSize(aSize));
1469 LookupResult result = LookupFrame(size, flags, ToPlaybackType(aWhichFrame),
1470 /* aMarkUsed = */ true);
1471 if (!result) {
1472 // Getting the frame (above) touches the image and kicks off decoding.
1473 if (mDrawStartTime.IsNull()) {
1474 mDrawStartTime = TimeStamp::Now();
1476 return ImgDrawResult::NOT_READY;
1479 bool shouldRecordTelemetry =
1480 !mDrawStartTime.IsNull() && result.Surface()->IsFinished();
1482 ImgDrawResult drawResult;
1484 gfxContextMatrixAutoSaveRestore asr;
1485 ImageRegion region(aRegion);
1487 if (!UsedOrientation().IsIdentity()) {
1488 // Apply a transform so that the unoriented image is drawn in the
1489 // orientation expected by the caller.
1490 gfxMatrix matrix = OrientationMatrix(size);
1491 asr.SetContext(aContext);
1492 aContext->Multiply(matrix);
1494 // Convert the region to unoriented coordinates.
1495 gfxMatrix inverseMatrix = OrientationMatrix(size, /* aInvert = */ true);
1496 region.TransformBoundsBy(inverseMatrix);
1499 drawResult = DrawInternal(std::move(result.Surface()), aContext, size,
1500 region, aSamplingFilter, flags, aOpacity);
1503 if (shouldRecordTelemetry) {
1504 TimeDuration drawLatency = TimeStamp::Now() - mDrawStartTime;
1505 Telemetry::Accumulate(Telemetry::IMAGE_DECODE_ON_DRAW_LATENCY,
1506 int32_t(drawLatency.ToMicroseconds()));
1507 mDrawStartTime = TimeStamp();
1510 return drawResult;
1513 //******************************************************************************
1515 NS_IMETHODIMP
1516 RasterImage::LockImage() {
1517 MOZ_ASSERT(NS_IsMainThread(),
1518 "Main thread to encourage serialization with UnlockImage");
1519 if (mError) {
1520 return NS_ERROR_FAILURE;
1523 // Increment the lock count
1524 mLockCount++;
1526 // Lock this image's surfaces in the SurfaceCache.
1527 if (mLockCount == 1) {
1528 SurfaceCache::LockImage(ImageKey(this));
1531 return NS_OK;
1534 //******************************************************************************
1536 NS_IMETHODIMP
1537 RasterImage::UnlockImage() {
1538 MOZ_ASSERT(NS_IsMainThread(),
1539 "Main thread to encourage serialization with LockImage");
1540 if (mError) {
1541 return NS_ERROR_FAILURE;
1544 // It's an error to call this function if the lock count is 0
1545 MOZ_ASSERT(mLockCount > 0, "Calling UnlockImage with mLockCount == 0!");
1546 if (mLockCount == 0) {
1547 return NS_ERROR_ABORT;
1550 // Decrement our lock count
1551 mLockCount--;
1553 // Unlock this image's surfaces in the SurfaceCache.
1554 if (mLockCount == 0) {
1555 SurfaceCache::UnlockImage(ImageKey(this));
1558 return NS_OK;
1561 //******************************************************************************
1563 NS_IMETHODIMP
1564 RasterImage::RequestDiscard() {
1565 if (mDiscardable && // Enabled at creation time...
1566 mLockCount == 0 && // ...not temporarily disabled...
1567 CanDiscard()) {
1568 Discard();
1571 return NS_OK;
1574 // Idempotent error flagging routine. If a decoder is open, shuts it down.
1575 void RasterImage::DoError() {
1576 // If we've flagged an error before, we have nothing to do
1577 if (mError) {
1578 return;
1581 // We can't safely handle errors off-main-thread, so dispatch a worker to
1582 // do it.
1583 if (!NS_IsMainThread()) {
1584 HandleErrorWorker::DispatchIfNeeded(this);
1585 return;
1588 // Put the container in an error state.
1589 mError = true;
1591 // Stop animation and release our FrameAnimator.
1592 if (mAnimating) {
1593 StopAnimation();
1595 mAnimationState = Nothing();
1596 mFrameAnimator = nullptr;
1598 // Release all locks.
1599 mLockCount = 0;
1600 SurfaceCache::UnlockImage(ImageKey(this));
1602 // Release all frames from the surface cache.
1603 SurfaceCache::RemoveImage(ImageKey(this));
1605 // Invalidate to get rid of any partially-drawn image content.
1606 auto dirtyRect = UnorientedIntRect({0, 0}, ToUnoriented(mSize));
1607 NotifyProgress(NoProgress, dirtyRect);
1609 MOZ_LOG(gImgLog, LogLevel::Error,
1610 ("RasterImage: [this=%p] Error detected for image\n", this));
1613 /* static */
1614 void RasterImage::HandleErrorWorker::DispatchIfNeeded(RasterImage* aImage) {
1615 RefPtr<HandleErrorWorker> worker = new HandleErrorWorker(aImage);
1616 NS_DispatchToMainThread(worker);
1619 RasterImage::HandleErrorWorker::HandleErrorWorker(RasterImage* aImage)
1620 : Runnable("image::RasterImage::HandleErrorWorker"), mImage(aImage) {
1621 MOZ_ASSERT(mImage, "Should have image");
1624 NS_IMETHODIMP
1625 RasterImage::HandleErrorWorker::Run() {
1626 mImage->DoError();
1628 return NS_OK;
1631 bool RasterImage::ShouldAnimate() {
1632 return ImageResource::ShouldAnimate() && mAnimationState &&
1633 mAnimationState->KnownFrameCount() >= 1 && !mAnimationFinished;
1636 #ifdef DEBUG
1637 NS_IMETHODIMP
1638 RasterImage::GetFramesNotified(uint32_t* aFramesNotified) {
1639 NS_ENSURE_ARG_POINTER(aFramesNotified);
1641 *aFramesNotified = mFramesNotified;
1643 return NS_OK;
1645 #endif
1647 void RasterImage::NotifyProgress(
1648 Progress aProgress,
1649 const UnorientedIntRect& aInvalidRect /* = UnorientedIntRect() */,
1650 const Maybe<uint32_t>& aFrameCount /* = Nothing() */,
1651 DecoderFlags aDecoderFlags /* = DefaultDecoderFlags() */,
1652 SurfaceFlags aSurfaceFlags /* = DefaultSurfaceFlags() */) {
1653 MOZ_ASSERT(NS_IsMainThread());
1655 // Ensure that we stay alive long enough to finish notifying.
1656 RefPtr<RasterImage> image = this;
1658 UnorientedIntRect invalidRect = aInvalidRect;
1660 if (!(aDecoderFlags & DecoderFlags::FIRST_FRAME_ONLY)) {
1661 // We may have decoded new animation frames; update our animation state.
1662 MOZ_ASSERT_IF(aFrameCount && *aFrameCount > 1, mAnimationState || mError);
1663 if (mAnimationState && aFrameCount) {
1664 mAnimationState->UpdateKnownFrameCount(*aFrameCount);
1667 // If we should start animating right now, do so.
1668 if (mAnimationState && aFrameCount == Some(1u) && mPendingAnimation &&
1669 ShouldAnimate()) {
1670 StartAnimation();
1673 if (mAnimationState) {
1674 auto size = ToUnoriented(mSize);
1675 IntRect rect = mAnimationState->UpdateState(this, size.ToUnknownSize());
1677 invalidRect.UnionRect(invalidRect,
1678 UnorientedIntRect::FromUnknownRect(rect));
1682 const bool wasDefaultFlags = aSurfaceFlags == DefaultSurfaceFlags();
1684 auto orientedInvalidRect = ToOriented(invalidRect);
1686 if (!orientedInvalidRect.IsEmpty() && wasDefaultFlags) {
1687 // Update our image container since we're invalidating.
1688 UpdateImageContainer(Some(orientedInvalidRect.ToUnknownRect()));
1691 // Tell the observers what happened.
1692 image->mProgressTracker->SyncNotifyProgress(
1693 aProgress, orientedInvalidRect.ToUnknownRect());
1696 void RasterImage::NotifyDecodeComplete(
1697 const DecoderFinalStatus& aStatus, const ImageMetadata& aMetadata,
1698 const DecoderTelemetry& aTelemetry, Progress aProgress,
1699 const UnorientedIntRect& aInvalidRect, const Maybe<uint32_t>& aFrameCount,
1700 DecoderFlags aDecoderFlags, SurfaceFlags aSurfaceFlags) {
1701 MOZ_ASSERT(NS_IsMainThread());
1703 // If the decoder detected an error, log it to the error console.
1704 if (aStatus.mShouldReportError) {
1705 ReportDecoderError();
1708 // Record all the metadata the decoder gathered about this image.
1709 bool metadataOK = SetMetadata(aMetadata, aStatus.mWasMetadataDecode);
1710 if (!metadataOK) {
1711 // This indicates a serious error that requires us to discard all existing
1712 // surfaces and redecode to recover. We'll drop the results from this
1713 // decoder on the floor, since they aren't valid.
1714 RecoverFromInvalidFrames(ToUnoriented(mSize),
1715 FromSurfaceFlags(aSurfaceFlags));
1716 return;
1719 MOZ_ASSERT(mError || mHasSize || !aMetadata.HasSize(),
1720 "SetMetadata should've gotten a size");
1722 if (!aStatus.mWasMetadataDecode && aStatus.mFinished) {
1723 // Flag that we've been decoded before.
1724 mHasBeenDecoded = true;
1727 // Send out any final notifications.
1728 NotifyProgress(aProgress, aInvalidRect, aFrameCount, aDecoderFlags,
1729 aSurfaceFlags);
1731 if (!(aDecoderFlags & DecoderFlags::FIRST_FRAME_ONLY)) {
1732 // We may have decoded new animation frames; update our animation state.
1733 MOZ_ASSERT_IF(aFrameCount && *aFrameCount > 1, mAnimationState || mError);
1734 if (mAnimationState && aFrameCount) {
1735 mAnimationState->UpdateKnownFrameCount(*aFrameCount);
1738 // If we should start animating right now, do so.
1739 if (mAnimationState && aFrameCount == Some(1u) && mPendingAnimation &&
1740 ShouldAnimate()) {
1741 StartAnimation();
1744 if (mAnimationState && mHasBeenDecoded) {
1745 // We've finished a full decode of all animation frames and our
1746 // AnimationState has been notified about them all, so let it know not to
1747 // expect anymore.
1748 mAnimationState->NotifyDecodeComplete();
1750 auto size = ToUnoriented(mSize);
1751 IntRect rect = mAnimationState->UpdateState(this, size.ToUnknownSize());
1753 if (!rect.IsEmpty()) {
1754 auto dirtyRect = UnorientedIntRect::FromUnknownRect(rect);
1755 NotifyProgress(NoProgress, dirtyRect);
1760 // Do some telemetry if this isn't a metadata decode.
1761 if (!aStatus.mWasMetadataDecode) {
1762 if (aTelemetry.mChunkCount) {
1763 Telemetry::Accumulate(Telemetry::IMAGE_DECODE_CHUNKS,
1764 aTelemetry.mChunkCount);
1767 if (aStatus.mFinished) {
1768 Telemetry::Accumulate(Telemetry::IMAGE_DECODE_TIME,
1769 int32_t(aTelemetry.mDecodeTime.ToMicroseconds()));
1771 if (aTelemetry.mSpeedHistogram && aTelemetry.mBytesDecoded) {
1772 Telemetry::Accumulate(*aTelemetry.mSpeedHistogram, aTelemetry.Speed());
1777 // Only act on errors if we have no usable frames from the decoder.
1778 if (aStatus.mHadError &&
1779 (!mAnimationState || mAnimationState->KnownFrameCount() == 0)) {
1780 DoError();
1781 } else if (aStatus.mWasMetadataDecode && !mHasSize) {
1782 DoError();
1785 // XXX(aosmond): Can we get this far without mFinished == true?
1786 if (aStatus.mFinished && aStatus.mWasMetadataDecode) {
1787 // If we were waiting to fire the load event, go ahead and fire it now.
1788 if (mLoadProgress) {
1789 NotifyForLoadEvent(*mLoadProgress);
1790 mLoadProgress = Nothing();
1793 // If we were a metadata decode and a full decode was requested, do it.
1794 if (mWantFullDecode) {
1795 mWantFullDecode = false;
1796 RequestDecodeForSize(mSize.ToUnknownSize(),
1797 DECODE_FLAGS_DEFAULT | FLAG_HIGH_QUALITY_SCALING,
1798 FRAME_CURRENT);
1803 void RasterImage::ReportDecoderError() {
1804 nsCOMPtr<nsIConsoleService> consoleService =
1805 do_GetService(NS_CONSOLESERVICE_CONTRACTID);
1806 nsCOMPtr<nsIScriptError> errorObject =
1807 do_CreateInstance(NS_SCRIPTERROR_CONTRACTID);
1809 if (consoleService && errorObject) {
1810 nsAutoString msg(u"Image corrupt or truncated."_ns);
1811 nsAutoString src;
1812 if (GetURI()) {
1813 nsAutoCString uri;
1814 if (!GetSpecTruncatedTo1k(uri)) {
1815 msg += u" URI in this note truncated due to length."_ns;
1817 src = NS_ConvertUTF8toUTF16(uri);
1819 if (NS_SUCCEEDED(errorObject->InitWithWindowID(msg, src, EmptyString(), 0,
1820 0, nsIScriptError::errorFlag,
1821 "Image", InnerWindowID()))) {
1822 consoleService->LogMessage(errorObject);
1827 already_AddRefed<imgIContainer> RasterImage::Unwrap() {
1828 nsCOMPtr<imgIContainer> self(this);
1829 return self.forget();
1832 void RasterImage::PropagateUseCounters(dom::Document*) {
1833 // No use counters.
1836 IntSize RasterImage::OptimalImageSizeForDest(const gfxSize& aDest,
1837 uint32_t aWhichFrame,
1838 SamplingFilter aSamplingFilter,
1839 uint32_t aFlags) {
1840 MOZ_ASSERT(aDest.width >= 0 || ceil(aDest.width) <= INT32_MAX ||
1841 aDest.height >= 0 || ceil(aDest.height) <= INT32_MAX,
1842 "Unexpected destination size");
1844 if (mSize.IsEmpty() || aDest.IsEmpty()) {
1845 return IntSize(0, 0);
1848 auto dest = OrientedIntSize::FromUnknownSize(
1849 IntSize::Ceil(aDest.width, aDest.height));
1851 if (aSamplingFilter == SamplingFilter::GOOD &&
1852 CanDownscaleDuringDecode(ToUnoriented(dest), aFlags)) {
1853 return dest.ToUnknownSize();
1856 // We can't scale to this size. Use our intrinsic size for now.
1857 return mSize.ToUnknownSize();
1860 gfxMatrix RasterImage::OrientationMatrix(const UnorientedIntSize& aSize,
1861 bool aInvert) const {
1862 return OrientedImage::OrientationMatrix(UsedOrientation(),
1863 aSize.ToUnknownSize(), aInvert);
1867 * Rotate aRect by the given angle within the space specified by aSize.
1869 * For example, with aRect = [20, 10, 5, 5] and aSize = [100, 100], rotating
1870 * with Angle::D90 will result in aRect = [85, 20, 5, 5].
1872 static void Rotate(IntRect& aRect, const IntSize& aSize, Angle aAngle) {
1873 switch (aAngle) {
1874 case Angle::D0:
1875 break;
1876 case Angle::D90:
1877 aRect = {aSize.height - aRect.YMost(), aRect.x, aRect.height,
1878 aRect.width};
1879 break;
1880 case Angle::D180:
1881 aRect.MoveTo(aSize.width - aRect.XMost(), aSize.height - aRect.YMost());
1882 break;
1883 case Angle::D270:
1884 aRect = {aRect.y, aSize.width - aRect.XMost(), aRect.height, aRect.width};
1885 break;
1890 * Flip aRect along the central axis within aSize.
1892 * For example, with aRect = [20, 10, 5, 5] and aSize = [100, 100], flipping
1893 * with Flip::Horizontal will result in aRect = [75, 10, 5, 5].
1895 static void Flip(IntRect& aRect, const IntSize& aSize, Flip aFlip) {
1896 switch (aFlip) {
1897 case Flip::Unflipped:
1898 break;
1899 case Flip::Horizontal:
1900 aRect.x = aSize.width - aRect.XMost();
1901 break;
1905 OrientedIntRect RasterImage::ToOriented(UnorientedIntRect aRect) const {
1906 IntRect rect = aRect.ToUnknownRect();
1907 auto size = ToUnoriented(mSize);
1909 MOZ_ASSERT(!UsedOrientation().flipFirst,
1910 "flipFirst should only be used by OrientedImage");
1912 // UsedOrientation() specifies the transformation from a correctly oriented
1913 // image to the pixels stored in the file, so we need to rotate by the
1914 // negation of the given angle.
1915 Angle angle = Orientation::InvertAngle(UsedOrientation().rotation);
1916 Rotate(rect, size.ToUnknownSize(), angle);
1918 // Use mSize instead of size, since after the Rotate call, the size of the
1919 // space that rect is in has had its width and height swapped.
1920 Flip(rect, mSize.ToUnknownSize(), UsedOrientation().flip);
1922 return OrientedIntRect::FromUnknownRect(rect);
1925 UnorientedIntRect RasterImage::ToUnoriented(OrientedIntRect aRect) const {
1926 IntRect rect = aRect.ToUnknownRect();
1928 Flip(rect, mSize.ToUnknownSize(), UsedOrientation().flip);
1929 Rotate(rect, mSize.ToUnknownSize(), UsedOrientation().rotation);
1931 MOZ_ASSERT(!UsedOrientation().flipFirst,
1932 "flipFirst should only be used by OrientedImage");
1934 return UnorientedIntRect::FromUnknownRect(rect);
1937 } // namespace image
1938 } // namespace mozilla