Bug 1698786: part 1) Add some logging to `mozInlineSpellChecker`. r=masayuki
[gecko.git] / image / RasterImage.cpp
blobb16f6fce509120fb6b77cbdf1743d7bfe0fe47e1
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 StoreHandledOrientation(StaticPrefs::image_honor_orientation_metadata());
84 //******************************************************************************
85 RasterImage::~RasterImage() {
86 // Make sure our SourceBuffer is marked as complete. This will ensure that any
87 // outstanding decoders terminate.
88 if (!mSourceBuffer->IsComplete()) {
89 mSourceBuffer->Complete(NS_ERROR_ABORT);
92 // Release all frames from the surface cache.
93 SurfaceCache::RemoveImage(ImageKey(this));
95 // Record Telemetry.
96 Telemetry::Accumulate(Telemetry::IMAGE_DECODE_COUNT, mDecodeCount);
99 nsresult RasterImage::Init(const char* aMimeType, uint32_t aFlags) {
100 // We don't support re-initialization
101 if (mInitialized) {
102 return NS_ERROR_ILLEGAL_VALUE;
105 // Not sure an error can happen before init, but be safe
106 if (mError) {
107 return NS_ERROR_FAILURE;
110 // We want to avoid redecodes for transient images.
111 MOZ_ASSERT_IF(aFlags & INIT_FLAG_TRANSIENT,
112 !(aFlags & INIT_FLAG_DISCARDABLE));
114 // Store initialization data
115 StoreDiscardable(!!(aFlags & INIT_FLAG_DISCARDABLE));
116 StoreWantFullDecode(!!(aFlags & INIT_FLAG_DECODE_IMMEDIATELY));
117 StoreTransient(!!(aFlags & INIT_FLAG_TRANSIENT));
118 StoreSyncLoad(!!(aFlags & INIT_FLAG_SYNC_LOAD));
120 // Use the MIME type to select a decoder type, and make sure there *is* a
121 // decoder for this MIME type.
122 NS_ENSURE_ARG_POINTER(aMimeType);
123 mDecoderType = DecoderFactory::GetDecoderType(aMimeType);
124 if (mDecoderType == DecoderType::UNKNOWN) {
125 return NS_ERROR_FAILURE;
128 // Lock this image's surfaces in the SurfaceCache if we're not discardable.
129 if (!LoadDiscardable()) {
130 mLockCount++;
131 SurfaceCache::LockImage(ImageKey(this));
134 // Mark us as initialized
135 mInitialized = true;
137 return NS_OK;
140 //******************************************************************************
141 NS_IMETHODIMP_(void)
142 RasterImage::RequestRefresh(const TimeStamp& aTime) {
143 if (HadRecentRefresh(aTime)) {
144 return;
147 EvaluateAnimation();
149 if (!mAnimating) {
150 return;
153 RefreshResult res;
154 if (mAnimationState) {
155 MOZ_ASSERT(mFrameAnimator);
156 res = mFrameAnimator->RequestRefresh(*mAnimationState, aTime);
159 #ifdef DEBUG
160 if (res.mFrameAdvanced) {
161 mFramesNotified++;
163 #endif
165 // Notify listeners that our frame has actually changed, but do this only
166 // once for all frames that we've now passed (if AdvanceFrame() was called
167 // more than once).
168 if (!res.mDirtyRect.IsEmpty() || res.mFrameAdvanced) {
169 auto dirtyRect = UnorientedIntRect::FromUnknownRect(res.mDirtyRect);
170 NotifyProgress(NoProgress, dirtyRect);
173 if (res.mAnimationFinished) {
174 StoreAnimationFinished(true);
175 EvaluateAnimation();
179 //******************************************************************************
180 NS_IMETHODIMP
181 RasterImage::GetWidth(int32_t* aWidth) {
182 NS_ENSURE_ARG_POINTER(aWidth);
184 if (mError) {
185 *aWidth = 0;
186 return NS_ERROR_FAILURE;
189 *aWidth = mSize.width;
190 return NS_OK;
193 //******************************************************************************
194 NS_IMETHODIMP
195 RasterImage::GetHeight(int32_t* aHeight) {
196 NS_ENSURE_ARG_POINTER(aHeight);
198 if (mError) {
199 *aHeight = 0;
200 return NS_ERROR_FAILURE;
203 *aHeight = mSize.height;
204 return NS_OK;
207 //******************************************************************************
208 nsresult RasterImage::GetNativeSizes(nsTArray<IntSize>& aNativeSizes) const {
209 if (mError) {
210 return NS_ERROR_FAILURE;
213 aNativeSizes.Clear();
215 if (mNativeSizes.IsEmpty()) {
216 aNativeSizes.AppendElement(mSize.ToUnknownSize());
217 } else {
218 for (const auto& size : mNativeSizes) {
219 aNativeSizes.AppendElement(size.ToUnknownSize());
223 return NS_OK;
226 //******************************************************************************
227 size_t RasterImage::GetNativeSizesLength() const {
228 if (mError || !LoadHasSize()) {
229 return 0;
232 if (mNativeSizes.IsEmpty()) {
233 return 1;
236 return mNativeSizes.Length();
239 //******************************************************************************
240 NS_IMETHODIMP
241 RasterImage::GetIntrinsicSize(nsSize* aSize) {
242 if (mError) {
243 return NS_ERROR_FAILURE;
246 *aSize = nsSize(nsPresContext::CSSPixelsToAppUnits(mSize.width),
247 nsPresContext::CSSPixelsToAppUnits(mSize.height));
248 return NS_OK;
251 //******************************************************************************
252 Maybe<AspectRatio> RasterImage::GetIntrinsicRatio() {
253 if (mError) {
254 return Nothing();
257 return Some(AspectRatio::FromSize(mSize.width, mSize.height));
260 NS_IMETHODIMP_(Orientation)
261 RasterImage::GetOrientation() { return mOrientation; }
263 NS_IMETHODIMP_(bool)
264 RasterImage::HandledOrientation() { return LoadHandledOrientation(); }
266 //******************************************************************************
267 NS_IMETHODIMP
268 RasterImage::GetType(uint16_t* aType) {
269 NS_ENSURE_ARG_POINTER(aType);
271 *aType = imgIContainer::TYPE_RASTER;
272 return NS_OK;
275 NS_IMETHODIMP
276 RasterImage::GetProducerId(uint32_t* aId) {
277 NS_ENSURE_ARG_POINTER(aId);
279 *aId = ImageResource::GetImageProducerId();
280 return NS_OK;
283 LookupResult RasterImage::LookupFrameInternal(const UnorientedIntSize& aSize,
284 uint32_t aFlags,
285 PlaybackType aPlaybackType,
286 bool aMarkUsed) {
287 if (mAnimationState && aPlaybackType == PlaybackType::eAnimated) {
288 MOZ_ASSERT(mFrameAnimator);
289 MOZ_ASSERT(ToSurfaceFlags(aFlags) == DefaultSurfaceFlags(),
290 "Can't composite frames with non-default surface flags");
291 return mFrameAnimator->GetCompositedFrame(*mAnimationState, aMarkUsed);
294 SurfaceFlags surfaceFlags = ToSurfaceFlags(aFlags);
296 // We don't want any substitution for sync decodes, and substitution would be
297 // illegal when high quality downscaling is disabled, so we use
298 // SurfaceCache::Lookup in this case.
299 if ((aFlags & FLAG_SYNC_DECODE) || !(aFlags & FLAG_HIGH_QUALITY_SCALING)) {
300 return SurfaceCache::Lookup(
301 ImageKey(this),
302 RasterSurfaceKey(aSize.ToUnknownSize(), surfaceFlags,
303 PlaybackType::eStatic),
304 aMarkUsed);
307 // We'll return the best match we can find to the requested frame.
308 return SurfaceCache::LookupBestMatch(
309 ImageKey(this),
310 RasterSurfaceKey(aSize.ToUnknownSize(), surfaceFlags,
311 PlaybackType::eStatic),
312 aMarkUsed);
315 LookupResult RasterImage::LookupFrame(const UnorientedIntSize& aSize,
316 uint32_t aFlags,
317 PlaybackType aPlaybackType,
318 bool aMarkUsed) {
319 MOZ_ASSERT(NS_IsMainThread());
321 // If we're opaque, we don't need to care about premultiplied alpha, because
322 // that can only matter for frames with transparency.
323 if (IsOpaque()) {
324 aFlags &= ~FLAG_DECODE_NO_PREMULTIPLY_ALPHA;
327 UnorientedIntSize requestedSize =
328 CanDownscaleDuringDecode(aSize, aFlags) ? aSize : ToUnoriented(mSize);
329 if (requestedSize.IsEmpty()) {
330 // Can't decode to a surface of zero size.
331 return LookupResult(MatchType::NOT_FOUND);
334 LookupResult result =
335 LookupFrameInternal(requestedSize, aFlags, aPlaybackType, aMarkUsed);
337 if (!result && !LoadHasSize()) {
338 // We can't request a decode without knowing our intrinsic size. Give up.
339 return LookupResult(MatchType::NOT_FOUND);
342 const bool syncDecode = aFlags & FLAG_SYNC_DECODE;
343 const bool avoidRedecode = aFlags & FLAG_AVOID_REDECODE_FOR_SIZE;
344 if (result.Type() == MatchType::NOT_FOUND ||
345 (result.Type() == MatchType::SUBSTITUTE_BECAUSE_NOT_FOUND &&
346 !avoidRedecode) ||
347 (syncDecode && !avoidRedecode && !result)) {
348 // We don't have a copy of this frame, and there's no decoder working on
349 // one. (Or we're sync decoding and the existing decoder hasn't even started
350 // yet.) Trigger decoding so it'll be available next time.
351 MOZ_ASSERT(aPlaybackType != PlaybackType::eAnimated ||
352 StaticPrefs::image_mem_animated_discardable_AtStartup() ||
353 !mAnimationState || mAnimationState->KnownFrameCount() < 1,
354 "Animated frames should be locked");
356 // The surface cache may suggest the preferred size we are supposed to
357 // decode at. This should only happen if we accept substitutions.
358 if (!result.SuggestedSize().IsEmpty()) {
359 MOZ_ASSERT(!syncDecode && (aFlags & FLAG_HIGH_QUALITY_SCALING));
360 requestedSize =
361 UnorientedIntSize::FromUnknownSize(result.SuggestedSize());
364 bool ranSync = false, failed = false;
365 Decode(requestedSize, aFlags, aPlaybackType, ranSync, failed);
366 if (failed) {
367 result.SetFailedToRequestDecode();
370 // If we can or did sync decode, we should already have the frame.
371 if (ranSync || syncDecode) {
372 result =
373 LookupFrameInternal(requestedSize, aFlags, aPlaybackType, aMarkUsed);
377 if (!result) {
378 // We still weren't able to get a frame. Give up.
379 return result;
382 // Sync decoding guarantees that we got the frame, but if it's owned by an
383 // async decoder that's currently running, the contents of the frame may not
384 // be available yet. Make sure we get everything.
385 if (LoadAllSourceData() && syncDecode) {
386 result.Surface()->WaitUntilFinished();
389 // If we could have done some decoding in this function we need to check if
390 // that decoding encountered an error and hence aborted the surface. We want
391 // to avoid calling IsAborted if we weren't passed any sync decode flag
392 // because IsAborted acquires the monitor for the imgFrame.
393 if (aFlags & (FLAG_SYNC_DECODE | FLAG_SYNC_DECODE_IF_FAST) &&
394 result.Surface()->IsAborted()) {
395 DrawableSurface tmp = std::move(result.Surface());
396 return result;
399 return result;
402 bool RasterImage::IsOpaque() {
403 if (mError) {
404 return false;
407 Progress progress = mProgressTracker->GetProgress();
409 // If we haven't yet finished decoding, the safe answer is "not opaque".
410 if (!(progress & FLAG_DECODE_COMPLETE)) {
411 return false;
414 // Other, we're opaque if FLAG_HAS_TRANSPARENCY is not set.
415 return !(progress & FLAG_HAS_TRANSPARENCY);
418 NS_IMETHODIMP_(bool)
419 RasterImage::WillDrawOpaqueNow() {
420 if (!IsOpaque()) {
421 return false;
424 if (mAnimationState) {
425 if (!StaticPrefs::image_mem_animated_discardable_AtStartup()) {
426 // We never discard frames of animated images.
427 return true;
428 } else {
429 if (mAnimationState->GetCompositedFrameInvalid()) {
430 // We're not going to draw anything at all.
431 return false;
436 // If we are not locked our decoded data could get discard at any time (ie
437 // between the call to this function and when we are asked to draw), so we
438 // have to return false if we are unlocked.
439 if (mLockCount == 0) {
440 return false;
443 auto size = ToUnoriented(mSize);
444 LookupResult result = SurfaceCache::LookupBestMatch(
445 ImageKey(this),
446 RasterSurfaceKey(size.ToUnknownSize(), DefaultSurfaceFlags(),
447 PlaybackType::eStatic),
448 /* aMarkUsed = */ false);
449 MatchType matchType = result.Type();
450 if (matchType == MatchType::NOT_FOUND || matchType == MatchType::PENDING ||
451 !result.Surface()->IsFinished()) {
452 return false;
455 return true;
458 void RasterImage::OnSurfaceDiscarded(const SurfaceKey& aSurfaceKey) {
459 MOZ_ASSERT(mProgressTracker);
461 bool animatedFramesDiscarded =
462 mAnimationState && aSurfaceKey.Playback() == PlaybackType::eAnimated;
464 nsCOMPtr<nsIEventTarget> eventTarget;
465 if (mProgressTracker) {
466 eventTarget = mProgressTracker->GetEventTarget();
467 } else {
468 eventTarget = do_GetMainThread();
471 RefPtr<RasterImage> image = this;
472 nsCOMPtr<nsIRunnable> ev =
473 NS_NewRunnableFunction("RasterImage::OnSurfaceDiscarded", [=]() -> void {
474 image->OnSurfaceDiscardedInternal(animatedFramesDiscarded);
476 eventTarget->Dispatch(ev.forget(), NS_DISPATCH_NORMAL);
479 void RasterImage::OnSurfaceDiscardedInternal(bool aAnimatedFramesDiscarded) {
480 MOZ_ASSERT(NS_IsMainThread());
482 if (aAnimatedFramesDiscarded && mAnimationState) {
483 MOZ_ASSERT(StaticPrefs::image_mem_animated_discardable_AtStartup());
484 ReleaseImageContainer();
486 auto size = ToUnoriented(mSize);
487 IntRect rect = mAnimationState->UpdateState(this, size.ToUnknownSize());
489 auto dirtyRect = UnorientedIntRect::FromUnknownRect(rect);
490 NotifyProgress(NoProgress, dirtyRect);
493 if (mProgressTracker) {
494 mProgressTracker->OnDiscard();
498 //******************************************************************************
499 NS_IMETHODIMP
500 RasterImage::GetAnimated(bool* aAnimated) {
501 if (mError) {
502 return NS_ERROR_FAILURE;
505 NS_ENSURE_ARG_POINTER(aAnimated);
507 // If we have an AnimationState, we can know for sure.
508 if (mAnimationState) {
509 *aAnimated = true;
510 return NS_OK;
513 // Otherwise, we need to have been decoded to know for sure, since if we were
514 // decoded at least once mAnimationState would have been created for animated
515 // images. This is true even though we check for animation during the
516 // metadata decode, because we may still discover animation only during the
517 // full decode for corrupt images.
518 if (!LoadHasBeenDecoded()) {
519 return NS_ERROR_NOT_AVAILABLE;
522 // We know for sure
523 *aAnimated = false;
525 return NS_OK;
528 //******************************************************************************
529 NS_IMETHODIMP_(int32_t)
530 RasterImage::GetFirstFrameDelay() {
531 if (mError) {
532 return -1;
535 bool animated = false;
536 if (NS_FAILED(GetAnimated(&animated)) || !animated) {
537 return -1;
540 MOZ_ASSERT(mAnimationState, "Animated images should have an AnimationState");
541 return mAnimationState->FirstFrameTimeout().AsEncodedValueDeprecated();
544 NS_IMETHODIMP_(already_AddRefed<SourceSurface>)
545 RasterImage::GetFrame(uint32_t aWhichFrame, uint32_t aFlags) {
546 return GetFrameAtSize(mSize.ToUnknownSize(), aWhichFrame, aFlags);
549 NS_IMETHODIMP_(already_AddRefed<SourceSurface>)
550 RasterImage::GetFrameAtSize(const IntSize& aSize, uint32_t aWhichFrame,
551 uint32_t aFlags) {
552 #ifdef DEBUG
553 NotifyDrawingObservers();
554 #endif
556 auto result = GetFrameInternal(aSize, Nothing(), aWhichFrame, aFlags);
557 return mozilla::Get<2>(result).forget();
560 Tuple<ImgDrawResult, IntSize, RefPtr<SourceSurface>>
561 RasterImage::GetFrameInternal(const IntSize& aSize,
562 const Maybe<SVGImageContext>& aSVGContext,
563 uint32_t aWhichFrame, uint32_t aFlags) {
564 MOZ_ASSERT(aWhichFrame <= FRAME_MAX_VALUE);
566 auto size = OrientedIntSize::FromUnknownSize(aSize);
568 if (aSize.IsEmpty() || aWhichFrame > FRAME_MAX_VALUE) {
569 return MakeTuple(ImgDrawResult::BAD_ARGS, aSize, RefPtr<SourceSurface>());
572 if (mError) {
573 return MakeTuple(ImgDrawResult::BAD_IMAGE, aSize, RefPtr<SourceSurface>());
576 // Get the frame. If it's not there, it's probably the caller's fault for
577 // not waiting for the data to be loaded from the network or not passing
578 // FLAG_SYNC_DECODE.
579 LookupResult result =
580 LookupFrame(ToUnoriented(size), aFlags, ToPlaybackType(aWhichFrame),
581 /* aMarkUsed = */ true);
582 auto resultSuggestedSize =
583 UnorientedIntSize::FromUnknownSize(result.SuggestedSize());
585 // The surface cache may have suggested we use a different size than the
586 // given size in the future. This may or may not be accompanied by an
587 // actual surface, depending on what it has in its cache.
588 OrientedIntSize suggestedSize = ToOriented(resultSuggestedSize);
589 if (suggestedSize.IsEmpty()) {
590 suggestedSize = size;
592 MOZ_ASSERT_IF(result.Type() == MatchType::SUBSTITUTE_BECAUSE_BEST,
593 suggestedSize != size);
595 if (!result) {
596 // The OS threw this frame away and we couldn't redecode it.
597 return MakeTuple(ImgDrawResult::TEMPORARY_ERROR,
598 suggestedSize.ToUnknownSize(), RefPtr<SourceSurface>());
601 RefPtr<SourceSurface> surface = result.Surface()->GetSourceSurface();
603 // If this RasterImage requires orientation, we must return a newly created
604 // surface with the oriented image instead of returning the frame's surface
605 // directly.
606 surface = OrientedImage::OrientSurface(UsedOrientation(), surface);
608 if (!result.Surface()->IsFinished()) {
609 return MakeTuple(ImgDrawResult::INCOMPLETE, suggestedSize.ToUnknownSize(),
610 std::move(surface));
613 return MakeTuple(ImgDrawResult::SUCCESS, suggestedSize.ToUnknownSize(),
614 std::move(surface));
617 Tuple<ImgDrawResult, IntSize> RasterImage::GetImageContainerSize(
618 LayerManager* aManager, const IntSize& aRequestedSize, uint32_t aFlags) {
619 if (!LoadHasSize()) {
620 return MakeTuple(ImgDrawResult::NOT_READY, IntSize(0, 0));
623 if (aRequestedSize.IsEmpty()) {
624 return MakeTuple(ImgDrawResult::BAD_ARGS, IntSize(0, 0));
627 // We check the minimum size because while we support downscaling, we do not
628 // support upscaling. If aRequestedSize > mSize, we will never give a larger
629 // surface than mSize. If mSize > aRequestedSize, and mSize > maxTextureSize,
630 // we still want to use image containers if aRequestedSize <= maxTextureSize.
631 int32_t maxTextureSize = aManager->GetMaxTextureSize();
632 if (min(mSize.width, aRequestedSize.width) > maxTextureSize ||
633 min(mSize.height, aRequestedSize.height) > maxTextureSize) {
634 return MakeTuple(ImgDrawResult::NOT_SUPPORTED, IntSize(0, 0));
637 auto requestedSize = OrientedIntSize::FromUnknownSize(aRequestedSize);
638 if (!CanDownscaleDuringDecode(ToUnoriented(requestedSize), aFlags)) {
639 return MakeTuple(ImgDrawResult::SUCCESS, mSize.ToUnknownSize());
642 return MakeTuple(ImgDrawResult::SUCCESS, aRequestedSize);
645 NS_IMETHODIMP_(bool)
646 RasterImage::IsImageContainerAvailable(LayerManager* aManager,
647 uint32_t aFlags) {
648 return IsImageContainerAvailableAtSize(aManager, mSize.ToUnknownSize(),
649 aFlags);
652 NS_IMETHODIMP_(already_AddRefed<ImageContainer>)
653 RasterImage::GetImageContainer(LayerManager* aManager, uint32_t aFlags) {
654 RefPtr<ImageContainer> container;
655 ImgDrawResult drawResult =
656 GetImageContainerImpl(aManager, mSize.ToUnknownSize(), Nothing(), aFlags,
657 getter_AddRefs(container));
659 // We silence the unused warning here because anything that needs the draw
660 // result should be using GetImageContainerAtSize, not GetImageContainer.
661 (void)drawResult;
662 return container.forget();
665 NS_IMETHODIMP_(bool)
666 RasterImage::IsImageContainerAvailableAtSize(LayerManager* aManager,
667 const IntSize& aRequestedSize,
668 uint32_t aFlags) {
669 // We check the minimum size because while we support downscaling, we do not
670 // support upscaling. If aRequestedSize > mSize, we will never give a larger
671 // surface than mSize. If mSize > aRequestedSize, and mSize > maxTextureSize,
672 // we still want to use image containers if aRequestedSize <= maxTextureSize.
673 int32_t maxTextureSize = aManager->GetMaxTextureSize();
674 if (!LoadHasSize() || aRequestedSize.IsEmpty() ||
675 min(mSize.width, aRequestedSize.width) > maxTextureSize ||
676 min(mSize.height, aRequestedSize.height) > maxTextureSize) {
677 return false;
680 return true;
683 NS_IMETHODIMP_(ImgDrawResult)
684 RasterImage::GetImageContainerAtSize(layers::LayerManager* aManager,
685 const gfx::IntSize& aSize,
686 const Maybe<SVGImageContext>& aSVGContext,
687 uint32_t aFlags,
688 layers::ImageContainer** aOutContainer) {
689 // We do not pass in the given SVG context because in theory it could differ
690 // between calls, but actually have no impact on the actual contents of the
691 // image container.
692 return GetImageContainerImpl(aManager, aSize, Nothing(), aFlags,
693 aOutContainer);
696 size_t RasterImage::SizeOfSourceWithComputedFallback(
697 SizeOfState& aState) const {
698 return mSourceBuffer->SizeOfIncludingThisWithComputedFallback(
699 aState.mMallocSizeOf);
702 void RasterImage::CollectSizeOfSurfaces(
703 nsTArray<SurfaceMemoryCounter>& aCounters,
704 MallocSizeOf aMallocSizeOf) const {
705 SurfaceCache::CollectSizeOfSurfaces(ImageKey(this), aCounters, aMallocSizeOf);
708 bool RasterImage::SetMetadata(const ImageMetadata& aMetadata,
709 bool aFromMetadataDecode) {
710 MOZ_ASSERT(NS_IsMainThread());
712 if (mError) {
713 return true;
716 if (aMetadata.HasSize()) {
717 auto metadataSize = UnorientedIntSize::FromUnknownSize(aMetadata.GetSize());
718 if (metadataSize.width < 0 || metadataSize.height < 0) {
719 NS_WARNING("Image has negative intrinsic size");
720 DoError();
721 return true;
724 MOZ_ASSERT(aMetadata.HasOrientation());
725 Orientation orientation = aMetadata.GetOrientation();
727 // If we already have a size, check the new size against the old one.
728 if (LoadHasSize() &&
729 (metadataSize != ToUnoriented(mSize) || orientation != mOrientation)) {
730 NS_WARNING(
731 "Image changed size or orientation on redecode! "
732 "This should not happen!");
733 DoError();
734 return true;
737 // Set the size and flag that we have it.
738 mOrientation = orientation;
739 mSize = ToOriented(metadataSize);
740 mNativeSizes.Clear();
741 for (const auto& nativeSize : aMetadata.GetNativeSizes()) {
742 mNativeSizes.AppendElement(
743 ToOriented(UnorientedIntSize::FromUnknownSize(nativeSize)));
745 StoreHasSize(true);
748 if (LoadHasSize() && aMetadata.HasAnimation() && !mAnimationState) {
749 // We're becoming animated, so initialize animation stuff.
750 mAnimationState.emplace(mAnimationMode);
751 mFrameAnimator =
752 MakeUnique<FrameAnimator>(this, ToUnoriented(mSize).ToUnknownSize());
754 if (!StaticPrefs::image_mem_animated_discardable_AtStartup()) {
755 // We don't support discarding animated images (See bug 414259).
756 // Lock the image and throw away the key.
757 LockImage();
760 if (!aFromMetadataDecode) {
761 // The metadata decode reported that this image isn't animated, but we
762 // discovered that it actually was during the full decode. This is a
763 // rare failure that only occurs for corrupt images. To recover, we need
764 // to discard all existing surfaces and redecode.
765 return false;
769 if (mAnimationState) {
770 mAnimationState->SetLoopCount(aMetadata.GetLoopCount());
771 mAnimationState->SetFirstFrameTimeout(aMetadata.GetFirstFrameTimeout());
773 if (aMetadata.HasLoopLength()) {
774 mAnimationState->SetLoopLength(aMetadata.GetLoopLength());
776 if (aMetadata.HasFirstFrameRefreshArea()) {
777 mAnimationState->SetFirstFrameRefreshArea(
778 aMetadata.GetFirstFrameRefreshArea());
782 if (aMetadata.HasHotspot()) {
783 // NOTE(heycam): We shouldn't have any image formats that support both
784 // orientation and hotspots, so we assert that rather than add code
785 // to orient the hotspot point correctly.
786 MOZ_ASSERT(UsedOrientation().IsIdentity(),
787 "Would need to orient hotspot point");
789 auto hotspot = aMetadata.GetHotspot();
790 mHotspot.x = std::max(std::min(hotspot.x, mSize.width - 1), 0);
791 mHotspot.y = std::max(std::min(hotspot.y, mSize.height - 1), 0);
794 return true;
797 NS_IMETHODIMP
798 RasterImage::SetAnimationMode(uint16_t aAnimationMode) {
799 if (mAnimationState) {
800 mAnimationState->SetAnimationMode(aAnimationMode);
802 return SetAnimationModeInternal(aAnimationMode);
805 //******************************************************************************
807 nsresult RasterImage::StartAnimation() {
808 if (mError) {
809 return NS_ERROR_FAILURE;
812 MOZ_ASSERT(ShouldAnimate(), "Should not animate!");
814 // If we're not ready to animate, then set mPendingAnimation, which will cause
815 // us to start animating if and when we do become ready.
816 StorePendingAnimation(!mAnimationState ||
817 mAnimationState->KnownFrameCount() < 1);
818 if (LoadPendingAnimation()) {
819 return NS_OK;
822 // Don't bother to animate if we're displaying the first frame forever.
823 if (mAnimationState->GetCurrentAnimationFrameIndex() == 0 &&
824 mAnimationState->FirstFrameTimeout() == FrameTimeout::Forever()) {
825 StoreAnimationFinished(true);
826 return NS_ERROR_ABORT;
829 // We need to set the time that this initial frame was first displayed, as
830 // this is used in AdvanceFrame().
831 mAnimationState->InitAnimationFrameTimeIfNecessary();
833 return NS_OK;
836 //******************************************************************************
837 nsresult RasterImage::StopAnimation() {
838 MOZ_ASSERT(mAnimating, "Should be animating!");
840 nsresult rv = NS_OK;
841 if (mError) {
842 rv = NS_ERROR_FAILURE;
843 } else {
844 mAnimationState->SetAnimationFrameTime(TimeStamp());
847 mAnimating = false;
848 return rv;
851 //******************************************************************************
852 NS_IMETHODIMP
853 RasterImage::ResetAnimation() {
854 if (mError) {
855 return NS_ERROR_FAILURE;
858 StorePendingAnimation(false);
860 if (mAnimationMode == kDontAnimMode || !mAnimationState ||
861 mAnimationState->GetCurrentAnimationFrameIndex() == 0) {
862 return NS_OK;
865 StoreAnimationFinished(false);
867 if (mAnimating) {
868 StopAnimation();
871 MOZ_ASSERT(mAnimationState, "Should have AnimationState");
872 MOZ_ASSERT(mFrameAnimator, "Should have FrameAnimator");
873 mFrameAnimator->ResetAnimation(*mAnimationState);
875 IntRect area = mAnimationState->FirstFrameRefreshArea();
876 NotifyProgress(NoProgress, UnorientedIntRect::FromUnknownRect(area));
878 // Start the animation again. It may not have been running before, if
879 // mAnimationFinished was true before entering this function.
880 EvaluateAnimation();
882 return NS_OK;
885 //******************************************************************************
886 NS_IMETHODIMP_(void)
887 RasterImage::SetAnimationStartTime(const TimeStamp& aTime) {
888 if (mError || mAnimationMode == kDontAnimMode || mAnimating ||
889 !mAnimationState) {
890 return;
893 mAnimationState->SetAnimationFrameTime(aTime);
896 NS_IMETHODIMP_(float)
897 RasterImage::GetFrameIndex(uint32_t aWhichFrame) {
898 MOZ_ASSERT(aWhichFrame <= FRAME_MAX_VALUE, "Invalid argument");
899 return (aWhichFrame == FRAME_FIRST || !mAnimationState)
900 ? 0.0f
901 : mAnimationState->GetCurrentAnimationFrameIndex();
904 NS_IMETHODIMP_(IntRect)
905 RasterImage::GetImageSpaceInvalidationRect(const IntRect& aRect) {
906 // Note that we do not transform aRect into an UnorientedIntRect, since
907 // RasterImage::NotifyProgress notifies all consumers of the image using
908 // OrientedIntRect values. (This is unlike OrientedImage, which notifies
909 // using inner image coordinates.)
910 return aRect;
913 nsresult RasterImage::OnImageDataComplete(nsIRequest*, nsISupports*,
914 nsresult aStatus, bool aLastPart) {
915 MOZ_ASSERT(NS_IsMainThread());
917 // Record that we have all the data we're going to get now.
918 StoreAllSourceData(true);
920 // Let decoders know that there won't be any more data coming.
921 mSourceBuffer->Complete(aStatus);
923 // Allow a synchronous metadata decode if mSyncLoad was set, or if we're
924 // running on a single thread (in which case waiting for the async metadata
925 // decoder could delay this image's load event quite a bit), or if this image
926 // is transient.
927 bool canSyncDecodeMetadata =
928 LoadSyncLoad() || LoadTransient() || DecodePool::NumberOfCores() < 2;
930 if (canSyncDecodeMetadata && !LoadHasSize()) {
931 // We're loading this image synchronously, so it needs to be usable after
932 // this call returns. Since we haven't gotten our size yet, we need to do a
933 // synchronous metadata decode here.
934 DecodeMetadata(FLAG_SYNC_DECODE);
937 // Determine our final status, giving precedence to Necko failure codes. We
938 // check after running the metadata decode in case it triggered an error.
939 nsresult finalStatus = mError ? NS_ERROR_FAILURE : NS_OK;
940 if (NS_FAILED(aStatus)) {
941 finalStatus = aStatus;
944 // If loading failed, report an error.
945 if (NS_FAILED(finalStatus)) {
946 DoError();
949 Progress loadProgress = LoadCompleteProgress(aLastPart, mError, finalStatus);
951 if (!LoadHasSize() && !mError) {
952 // We don't have our size yet, so we'll fire the load event in SetSize().
953 MOZ_ASSERT(!canSyncDecodeMetadata,
954 "Firing load async after metadata sync decode?");
955 mLoadProgress = Some(loadProgress);
956 return finalStatus;
959 NotifyForLoadEvent(loadProgress);
961 return finalStatus;
964 void RasterImage::NotifyForLoadEvent(Progress aProgress) {
965 MOZ_ASSERT(LoadHasSize() || mError,
966 "Need to know size before firing load event");
967 MOZ_ASSERT(
968 !LoadHasSize() || (mProgressTracker->GetProgress() & FLAG_SIZE_AVAILABLE),
969 "Should have notified that the size is available if we have it");
971 // If we encountered an error, make sure we notify for that as well.
972 if (mError) {
973 aProgress |= FLAG_HAS_ERROR;
976 // Notify our listeners, which will fire this image's load event.
977 NotifyProgress(aProgress);
980 nsresult RasterImage::OnImageDataAvailable(nsIRequest*, nsISupports*,
981 nsIInputStream* aInputStream,
982 uint64_t, uint32_t aCount) {
983 nsresult rv = mSourceBuffer->AppendFromInputStream(aInputStream, aCount);
984 if (NS_SUCCEEDED(rv) && !LoadSomeSourceData()) {
985 StoreSomeSourceData(true);
986 if (!LoadSyncLoad()) {
987 // Create an async metadata decoder and verify we succeed in doing so.
988 rv = DecodeMetadata(DECODE_FLAGS_DEFAULT);
992 if (NS_FAILED(rv)) {
993 DoError();
995 return rv;
998 nsresult RasterImage::SetSourceSizeHint(uint32_t aSizeHint) {
999 if (aSizeHint == 0) {
1000 return NS_OK;
1003 nsresult rv = mSourceBuffer->ExpectLength(aSizeHint);
1004 if (rv == NS_ERROR_OUT_OF_MEMORY) {
1005 // Flush memory, try to get some back, and try again.
1006 rv = nsMemory::HeapMinimize(true);
1007 if (NS_SUCCEEDED(rv)) {
1008 rv = mSourceBuffer->ExpectLength(aSizeHint);
1012 return rv;
1015 nsresult RasterImage::GetHotspotX(int32_t* aX) {
1016 *aX = mHotspot.x;
1017 return NS_OK;
1020 nsresult RasterImage::GetHotspotY(int32_t* aY) {
1021 *aY = mHotspot.y;
1022 return NS_OK;
1025 void RasterImage::Discard() {
1026 MOZ_ASSERT(NS_IsMainThread());
1027 MOZ_ASSERT(CanDiscard(), "Asked to discard but can't");
1028 MOZ_ASSERT(!mAnimationState ||
1029 StaticPrefs::image_mem_animated_discardable_AtStartup(),
1030 "Asked to discard for animated image");
1032 // Delete all the decoded frames.
1033 SurfaceCache::RemoveImage(ImageKey(this));
1035 if (mAnimationState) {
1036 ReleaseImageContainer();
1038 auto size = ToUnoriented(mSize);
1039 IntRect rect = mAnimationState->UpdateState(this, size.ToUnknownSize());
1041 auto dirtyRect = UnorientedIntRect::FromUnknownRect(rect);
1042 NotifyProgress(NoProgress, dirtyRect);
1045 // Notify that we discarded.
1046 if (mProgressTracker) {
1047 mProgressTracker->OnDiscard();
1051 bool RasterImage::CanDiscard() {
1052 return LoadAllSourceData() &&
1053 // Can discard animated images if the pref is set
1054 (!mAnimationState ||
1055 StaticPrefs::image_mem_animated_discardable_AtStartup());
1058 NS_IMETHODIMP
1059 RasterImage::StartDecoding(uint32_t aFlags, uint32_t aWhichFrame) {
1060 if (mError) {
1061 return NS_ERROR_FAILURE;
1064 if (!LoadHasSize()) {
1065 StoreWantFullDecode(true);
1066 return NS_OK;
1069 uint32_t flags = (aFlags & FLAG_ASYNC_NOTIFY) | FLAG_SYNC_DECODE_IF_FAST |
1070 FLAG_HIGH_QUALITY_SCALING;
1071 return RequestDecodeForSize(mSize.ToUnknownSize(), flags, aWhichFrame);
1074 bool RasterImage::StartDecodingWithResult(uint32_t aFlags,
1075 uint32_t aWhichFrame) {
1076 if (mError) {
1077 return false;
1080 if (!LoadHasSize()) {
1081 StoreWantFullDecode(true);
1082 return false;
1085 uint32_t flags = (aFlags & FLAG_ASYNC_NOTIFY) | FLAG_SYNC_DECODE_IF_FAST |
1086 FLAG_HIGH_QUALITY_SCALING;
1087 LookupResult result =
1088 RequestDecodeForSizeInternal(ToUnoriented(mSize), flags, aWhichFrame);
1089 DrawableSurface surface = std::move(result.Surface());
1090 return surface && surface->IsFinished();
1093 imgIContainer::DecodeResult RasterImage::RequestDecodeWithResult(
1094 uint32_t aFlags, uint32_t aWhichFrame) {
1095 MOZ_ASSERT(NS_IsMainThread());
1097 if (mError) {
1098 return imgIContainer::DECODE_REQUEST_FAILED;
1101 uint32_t flags = aFlags | FLAG_ASYNC_NOTIFY;
1102 LookupResult result =
1103 RequestDecodeForSizeInternal(ToUnoriented(mSize), flags, aWhichFrame);
1104 DrawableSurface surface = std::move(result.Surface());
1105 if (surface && surface->IsFinished()) {
1106 return imgIContainer::DECODE_SURFACE_AVAILABLE;
1108 if (result.GetFailedToRequestDecode()) {
1109 return imgIContainer::DECODE_REQUEST_FAILED;
1111 return imgIContainer::DECODE_REQUESTED;
1114 NS_IMETHODIMP
1115 RasterImage::RequestDecodeForSize(const IntSize& aSize, uint32_t aFlags,
1116 uint32_t aWhichFrame) {
1117 MOZ_ASSERT(NS_IsMainThread());
1119 if (mError) {
1120 return NS_ERROR_FAILURE;
1123 RequestDecodeForSizeInternal(
1124 ToUnoriented(OrientedIntSize::FromUnknownSize(aSize)), aFlags,
1125 aWhichFrame);
1127 return NS_OK;
1130 LookupResult RasterImage::RequestDecodeForSizeInternal(
1131 const UnorientedIntSize& aSize, uint32_t aFlags, uint32_t aWhichFrame) {
1132 MOZ_ASSERT(NS_IsMainThread());
1134 if (aWhichFrame > FRAME_MAX_VALUE) {
1135 return LookupResult(MatchType::NOT_FOUND);
1138 if (mError) {
1139 LookupResult result = LookupResult(MatchType::NOT_FOUND);
1140 result.SetFailedToRequestDecode();
1141 return result;
1144 if (!LoadHasSize()) {
1145 StoreWantFullDecode(true);
1146 return LookupResult(MatchType::NOT_FOUND);
1149 // Decide whether to sync decode images we can decode quickly. Here we are
1150 // explicitly trading off flashing for responsiveness in the case that we're
1151 // redecoding an image (see bug 845147).
1152 bool shouldSyncDecodeIfFast =
1153 !LoadHasBeenDecoded() && (aFlags & FLAG_SYNC_DECODE_IF_FAST);
1155 uint32_t flags =
1156 shouldSyncDecodeIfFast ? aFlags : aFlags & ~FLAG_SYNC_DECODE_IF_FAST;
1158 // Perform a frame lookup, which will implicitly start decoding if needed.
1159 return LookupFrame(aSize, flags, ToPlaybackType(aWhichFrame),
1160 /* aMarkUsed = */ false);
1163 static bool LaunchDecodingTask(IDecodingTask* aTask, RasterImage* aImage,
1164 uint32_t aFlags, bool aHaveSourceData) {
1165 if (aHaveSourceData) {
1166 nsCString uri(aImage->GetURIString());
1168 // If we have all the data, we can sync decode if requested.
1169 if (aFlags & imgIContainer::FLAG_SYNC_DECODE) {
1170 DecodePool::Singleton()->SyncRunIfPossible(aTask, uri);
1171 return true;
1174 if (aFlags & imgIContainer::FLAG_SYNC_DECODE_IF_FAST) {
1175 return DecodePool::Singleton()->SyncRunIfPreferred(aTask, uri);
1179 // Perform an async decode. We also take this path if we don't have all the
1180 // source data yet, since sync decoding is impossible in that situation.
1181 DecodePool::Singleton()->AsyncRun(aTask);
1182 return false;
1185 void RasterImage::Decode(const UnorientedIntSize& aSize, uint32_t aFlags,
1186 PlaybackType aPlaybackType, bool& aOutRanSync,
1187 bool& aOutFailed) {
1188 MOZ_ASSERT(NS_IsMainThread());
1190 if (mError) {
1191 aOutFailed = true;
1192 return;
1195 // If we don't have a size yet, we can't do any other decoding.
1196 if (!LoadHasSize()) {
1197 StoreWantFullDecode(true);
1198 return;
1201 // We're about to decode again, which may mean that some of the previous sizes
1202 // we've decoded at aren't useful anymore. We can allow them to expire from
1203 // the cache by unlocking them here. When the decode finishes, it will send an
1204 // invalidation that will cause all instances of this image to redraw. If this
1205 // image is locked, any surfaces that are still useful will become locked
1206 // again when LookupFrame touches them, and the remainder will eventually
1207 // expire.
1208 SurfaceCache::UnlockEntries(ImageKey(this));
1210 // Determine which flags we need to decode this image with.
1211 DecoderFlags decoderFlags = DefaultDecoderFlags();
1212 if (aFlags & FLAG_ASYNC_NOTIFY) {
1213 decoderFlags |= DecoderFlags::ASYNC_NOTIFY;
1215 if (LoadTransient()) {
1216 decoderFlags |= DecoderFlags::IMAGE_IS_TRANSIENT;
1218 if (LoadHasBeenDecoded()) {
1219 decoderFlags |= DecoderFlags::IS_REDECODE;
1221 if ((aFlags & FLAG_SYNC_DECODE) || !(aFlags & FLAG_HIGH_QUALITY_SCALING)) {
1222 // Used SurfaceCache::Lookup instead of SurfaceCache::LookupBestMatch. That
1223 // means the caller can handle a differently sized surface to be returned
1224 // at any point.
1225 decoderFlags |= DecoderFlags::CANNOT_SUBSTITUTE;
1228 SurfaceFlags surfaceFlags = ToSurfaceFlags(aFlags);
1229 if (IsOpaque()) {
1230 // If there's no transparency, it doesn't matter whether we premultiply
1231 // alpha or not.
1232 surfaceFlags &= ~SurfaceFlags::NO_PREMULTIPLY_ALPHA;
1235 // Create a decoder.
1236 RefPtr<IDecodingTask> task;
1237 nsresult rv;
1238 bool animated = mAnimationState && aPlaybackType == PlaybackType::eAnimated;
1239 if (animated) {
1240 size_t currentFrame = mAnimationState->GetCurrentAnimationFrameIndex();
1241 rv = DecoderFactory::CreateAnimationDecoder(
1242 mDecoderType, WrapNotNull(this), mSourceBuffer,
1243 ToUnoriented(mSize).ToUnknownSize(), decoderFlags, surfaceFlags,
1244 currentFrame, getter_AddRefs(task));
1245 } else {
1246 rv = DecoderFactory::CreateDecoder(
1247 mDecoderType, WrapNotNull(this), mSourceBuffer,
1248 ToUnoriented(mSize).ToUnknownSize(), aSize.ToUnknownSize(),
1249 decoderFlags, surfaceFlags, getter_AddRefs(task));
1252 if (rv == NS_ERROR_ALREADY_INITIALIZED) {
1253 // We raced with an already pending decoder, and it finished before we
1254 // managed to insert the new decoder. Pretend we did a sync call to make
1255 // the caller lookup in the surface cache again.
1256 MOZ_ASSERT(!task);
1257 aOutRanSync = true;
1258 return;
1261 if (animated) {
1262 // We pass false for aAllowInvalidation because we may be asked to use
1263 // async notifications. Any potential invalidation here will be sent when
1264 // RequestRefresh is called, or NotifyDecodeComplete.
1265 #ifdef DEBUG
1266 IntRect rect =
1267 #endif
1268 mAnimationState->UpdateState(this, ToUnoriented(mSize).ToUnknownSize(),
1269 false);
1270 MOZ_ASSERT(rect.IsEmpty());
1273 // Make sure DecoderFactory was able to create a decoder successfully.
1274 if (NS_FAILED(rv)) {
1275 MOZ_ASSERT(!task);
1276 aOutFailed = true;
1277 return;
1280 MOZ_ASSERT(task);
1281 mDecodeCount++;
1283 // We're ready to decode; start the decoder.
1284 aOutRanSync = LaunchDecodingTask(task, this, aFlags, LoadAllSourceData());
1287 NS_IMETHODIMP
1288 RasterImage::DecodeMetadata(uint32_t aFlags) {
1289 if (mError) {
1290 return NS_ERROR_FAILURE;
1293 MOZ_ASSERT(!LoadHasSize(), "Should not do unnecessary metadata decodes");
1295 // Create a decoder.
1296 RefPtr<IDecodingTask> task = DecoderFactory::CreateMetadataDecoder(
1297 mDecoderType, WrapNotNull(this), mSourceBuffer);
1299 // Make sure DecoderFactory was able to create a decoder successfully.
1300 if (!task) {
1301 return NS_ERROR_FAILURE;
1304 // We're ready to decode; start the decoder.
1305 LaunchDecodingTask(task, this, aFlags, LoadAllSourceData());
1306 return NS_OK;
1309 void RasterImage::RecoverFromInvalidFrames(const UnorientedIntSize& aSize,
1310 uint32_t aFlags) {
1311 if (!LoadHasSize()) {
1312 return;
1315 NS_WARNING("A RasterImage's frames became invalid. Attempting to recover...");
1317 // Discard all existing frames, since they're probably all now invalid.
1318 SurfaceCache::RemoveImage(ImageKey(this));
1320 // Relock the image if it's supposed to be locked.
1321 if (mLockCount > 0) {
1322 SurfaceCache::LockImage(ImageKey(this));
1325 bool unused1, unused2;
1327 // Animated images require some special handling, because we normally require
1328 // that they never be discarded.
1329 if (mAnimationState) {
1330 Decode(ToUnoriented(mSize), aFlags | FLAG_SYNC_DECODE,
1331 PlaybackType::eAnimated, unused1, unused2);
1332 ResetAnimation();
1333 return;
1336 // For non-animated images, it's fine to recover using an async decode.
1337 Decode(aSize, aFlags, PlaybackType::eStatic, unused1, unused2);
1340 static bool HaveSkia() {
1341 #ifdef MOZ_ENABLE_SKIA
1342 return true;
1343 #else
1344 return false;
1345 #endif
1348 bool RasterImage::CanDownscaleDuringDecode(const UnorientedIntSize& aSize,
1349 uint32_t aFlags) {
1350 // Check basic requirements: downscale-during-decode is enabled, Skia is
1351 // available, this image isn't transient, we have all the source data and know
1352 // our size, and the flags allow us to do it.
1353 if (!LoadHasSize() || LoadTransient() || !HaveSkia() ||
1354 !StaticPrefs::image_downscale_during_decode_enabled() ||
1355 !(aFlags & imgIContainer::FLAG_HIGH_QUALITY_SCALING)) {
1356 return false;
1359 // We don't downscale animated images during decode.
1360 if (mAnimationState) {
1361 return false;
1364 // Never upscale.
1365 UnorientedIntSize ourSize = ToUnoriented(mSize);
1366 if (aSize.width >= ourSize.width || aSize.height >= ourSize.height) {
1367 return false;
1370 // Zero or negative width or height is unacceptable.
1371 if (aSize.width < 1 || aSize.height < 1) {
1372 return false;
1375 // There's no point in scaling if we can't store the result.
1376 if (!SurfaceCache::CanHold(aSize.ToUnknownSize())) {
1377 return false;
1380 return true;
1383 ImgDrawResult RasterImage::DrawInternal(DrawableSurface&& aSurface,
1384 gfxContext* aContext,
1385 const UnorientedIntSize& aSize,
1386 const ImageRegion& aRegion,
1387 SamplingFilter aSamplingFilter,
1388 uint32_t aFlags, float aOpacity) {
1389 gfxContextMatrixAutoSaveRestore saveMatrix(aContext);
1390 ImageRegion region(aRegion);
1391 bool frameIsFinished = aSurface->IsFinished();
1393 #ifdef DEBUG
1394 NotifyDrawingObservers();
1395 #endif
1397 // By now we may have a frame with the requested size. If not, we need to
1398 // adjust the drawing parameters accordingly.
1399 IntSize finalSize = aSurface->GetSize();
1400 bool couldRedecodeForBetterFrame = false;
1401 if (finalSize != aSize.ToUnknownSize()) {
1402 gfx::Size scale(double(aSize.width) / finalSize.width,
1403 double(aSize.height) / finalSize.height);
1404 aContext->Multiply(gfxMatrix::Scaling(scale.width, scale.height));
1405 region.Scale(1.0 / scale.width, 1.0 / scale.height);
1407 couldRedecodeForBetterFrame = CanDownscaleDuringDecode(aSize, aFlags);
1410 if (!aSurface->Draw(aContext, region, aSamplingFilter, aFlags, aOpacity)) {
1411 RecoverFromInvalidFrames(aSize, aFlags);
1412 return ImgDrawResult::TEMPORARY_ERROR;
1414 if (!frameIsFinished) {
1415 return ImgDrawResult::INCOMPLETE;
1417 if (couldRedecodeForBetterFrame) {
1418 return ImgDrawResult::WRONG_SIZE;
1420 return ImgDrawResult::SUCCESS;
1423 //******************************************************************************
1424 NS_IMETHODIMP_(ImgDrawResult)
1425 RasterImage::Draw(gfxContext* aContext, const IntSize& aSize,
1426 const ImageRegion& aRegion, uint32_t aWhichFrame,
1427 SamplingFilter aSamplingFilter,
1428 const Maybe<SVGImageContext>& /*aSVGContext - ignored*/,
1429 uint32_t aFlags, float aOpacity) {
1430 if (aWhichFrame > FRAME_MAX_VALUE) {
1431 return ImgDrawResult::BAD_ARGS;
1434 if (mError) {
1435 return ImgDrawResult::BAD_IMAGE;
1438 // Illegal -- you can't draw with non-default decode flags.
1439 // (Disabling colorspace conversion might make sense to allow, but
1440 // we don't currently.)
1441 if (ToSurfaceFlags(aFlags) != DefaultSurfaceFlags()) {
1442 return ImgDrawResult::BAD_ARGS;
1445 if (!aContext) {
1446 return ImgDrawResult::BAD_ARGS;
1449 if (mAnimationConsumers == 0) {
1450 SendOnUnlockedDraw(aFlags);
1453 // If we're not using SamplingFilter::GOOD, we shouldn't high-quality scale or
1454 // downscale during decode.
1455 uint32_t flags = aSamplingFilter == SamplingFilter::GOOD
1456 ? aFlags
1457 : aFlags & ~FLAG_HIGH_QUALITY_SCALING;
1459 auto size = ToUnoriented(OrientedIntSize::FromUnknownSize(aSize));
1460 LookupResult result = LookupFrame(size, flags, ToPlaybackType(aWhichFrame),
1461 /* aMarkUsed = */ true);
1462 if (!result) {
1463 // Getting the frame (above) touches the image and kicks off decoding.
1464 if (mDrawStartTime.IsNull()) {
1465 mDrawStartTime = TimeStamp::Now();
1467 return ImgDrawResult::NOT_READY;
1470 bool shouldRecordTelemetry =
1471 !mDrawStartTime.IsNull() && result.Surface()->IsFinished();
1473 ImgDrawResult drawResult;
1475 gfxContextMatrixAutoSaveRestore asr;
1476 ImageRegion region(aRegion);
1478 if (!UsedOrientation().IsIdentity()) {
1479 // Apply a transform so that the unoriented image is drawn in the
1480 // orientation expected by the caller.
1481 gfxMatrix matrix = OrientationMatrix(size);
1482 asr.SetContext(aContext);
1483 aContext->Multiply(matrix);
1485 // Convert the region to unoriented coordinates.
1486 gfxMatrix inverseMatrix = OrientationMatrix(size, /* aInvert = */ true);
1487 region.TransformBoundsBy(inverseMatrix);
1490 drawResult = DrawInternal(std::move(result.Surface()), aContext, size,
1491 region, aSamplingFilter, flags, aOpacity);
1494 if (shouldRecordTelemetry) {
1495 TimeDuration drawLatency = TimeStamp::Now() - mDrawStartTime;
1496 Telemetry::Accumulate(Telemetry::IMAGE_DECODE_ON_DRAW_LATENCY,
1497 int32_t(drawLatency.ToMicroseconds()));
1498 mDrawStartTime = TimeStamp();
1501 return drawResult;
1504 //******************************************************************************
1506 NS_IMETHODIMP
1507 RasterImage::LockImage() {
1508 MOZ_ASSERT(NS_IsMainThread(),
1509 "Main thread to encourage serialization with UnlockImage");
1510 if (mError) {
1511 return NS_ERROR_FAILURE;
1514 // Increment the lock count
1515 mLockCount++;
1517 // Lock this image's surfaces in the SurfaceCache.
1518 if (mLockCount == 1) {
1519 SurfaceCache::LockImage(ImageKey(this));
1522 return NS_OK;
1525 //******************************************************************************
1527 NS_IMETHODIMP
1528 RasterImage::UnlockImage() {
1529 MOZ_ASSERT(NS_IsMainThread(),
1530 "Main thread to encourage serialization with LockImage");
1531 if (mError) {
1532 return NS_ERROR_FAILURE;
1535 // It's an error to call this function if the lock count is 0
1536 MOZ_ASSERT(mLockCount > 0, "Calling UnlockImage with mLockCount == 0!");
1537 if (mLockCount == 0) {
1538 return NS_ERROR_ABORT;
1541 // Decrement our lock count
1542 mLockCount--;
1544 // Unlock this image's surfaces in the SurfaceCache.
1545 if (mLockCount == 0) {
1546 SurfaceCache::UnlockImage(ImageKey(this));
1549 return NS_OK;
1552 //******************************************************************************
1554 NS_IMETHODIMP
1555 RasterImage::RequestDiscard() {
1556 if (LoadDiscardable() && // Enabled at creation time...
1557 mLockCount == 0 && // ...not temporarily disabled...
1558 CanDiscard()) {
1559 Discard();
1562 return NS_OK;
1565 // Idempotent error flagging routine. If a decoder is open, shuts it down.
1566 void RasterImage::DoError() {
1567 // If we've flagged an error before, we have nothing to do
1568 if (mError) {
1569 return;
1572 // We can't safely handle errors off-main-thread, so dispatch a worker to
1573 // do it.
1574 if (!NS_IsMainThread()) {
1575 HandleErrorWorker::DispatchIfNeeded(this);
1576 return;
1579 // Put the container in an error state.
1580 mError = true;
1582 // Stop animation and release our FrameAnimator.
1583 if (mAnimating) {
1584 StopAnimation();
1586 mAnimationState = Nothing();
1587 mFrameAnimator = nullptr;
1589 // Release all locks.
1590 mLockCount = 0;
1591 SurfaceCache::UnlockImage(ImageKey(this));
1593 // Release all frames from the surface cache.
1594 SurfaceCache::RemoveImage(ImageKey(this));
1596 // Invalidate to get rid of any partially-drawn image content.
1597 auto dirtyRect = UnorientedIntRect({0, 0}, ToUnoriented(mSize));
1598 NotifyProgress(NoProgress, dirtyRect);
1600 MOZ_LOG(gImgLog, LogLevel::Error,
1601 ("RasterImage: [this=%p] Error detected for image\n", this));
1604 /* static */
1605 void RasterImage::HandleErrorWorker::DispatchIfNeeded(RasterImage* aImage) {
1606 RefPtr<HandleErrorWorker> worker = new HandleErrorWorker(aImage);
1607 NS_DispatchToMainThread(worker);
1610 RasterImage::HandleErrorWorker::HandleErrorWorker(RasterImage* aImage)
1611 : Runnable("image::RasterImage::HandleErrorWorker"), mImage(aImage) {
1612 MOZ_ASSERT(mImage, "Should have image");
1615 NS_IMETHODIMP
1616 RasterImage::HandleErrorWorker::Run() {
1617 mImage->DoError();
1619 return NS_OK;
1622 bool RasterImage::ShouldAnimate() {
1623 return ImageResource::ShouldAnimate() && mAnimationState &&
1624 mAnimationState->KnownFrameCount() >= 1 && !LoadAnimationFinished();
1627 #ifdef DEBUG
1628 NS_IMETHODIMP
1629 RasterImage::GetFramesNotified(uint32_t* aFramesNotified) {
1630 NS_ENSURE_ARG_POINTER(aFramesNotified);
1632 *aFramesNotified = mFramesNotified;
1634 return NS_OK;
1636 #endif
1638 void RasterImage::NotifyProgress(
1639 Progress aProgress,
1640 const UnorientedIntRect& aInvalidRect /* = UnorientedIntRect() */,
1641 const Maybe<uint32_t>& aFrameCount /* = Nothing() */,
1642 DecoderFlags aDecoderFlags /* = DefaultDecoderFlags() */,
1643 SurfaceFlags aSurfaceFlags /* = DefaultSurfaceFlags() */) {
1644 MOZ_ASSERT(NS_IsMainThread());
1646 // Ensure that we stay alive long enough to finish notifying.
1647 RefPtr<RasterImage> image = this;
1649 UnorientedIntRect invalidRect = aInvalidRect;
1651 if (!(aDecoderFlags & DecoderFlags::FIRST_FRAME_ONLY)) {
1652 // We may have decoded new animation frames; update our animation state.
1653 MOZ_ASSERT_IF(aFrameCount && *aFrameCount > 1, mAnimationState || mError);
1654 if (mAnimationState && aFrameCount) {
1655 mAnimationState->UpdateKnownFrameCount(*aFrameCount);
1658 // If we should start animating right now, do so.
1659 if (mAnimationState && aFrameCount == Some(1u) && LoadPendingAnimation() &&
1660 ShouldAnimate()) {
1661 StartAnimation();
1664 if (mAnimationState) {
1665 auto size = ToUnoriented(mSize);
1666 IntRect rect = mAnimationState->UpdateState(this, size.ToUnknownSize());
1668 invalidRect.UnionRect(invalidRect,
1669 UnorientedIntRect::FromUnknownRect(rect));
1673 const bool wasDefaultFlags = aSurfaceFlags == DefaultSurfaceFlags();
1675 auto orientedInvalidRect = ToOriented(invalidRect);
1677 if (!orientedInvalidRect.IsEmpty() && wasDefaultFlags) {
1678 // Update our image container since we're invalidating.
1679 UpdateImageContainer(Some(orientedInvalidRect.ToUnknownRect()));
1682 // Tell the observers what happened.
1683 image->mProgressTracker->SyncNotifyProgress(
1684 aProgress, orientedInvalidRect.ToUnknownRect());
1687 void RasterImage::NotifyDecodeComplete(
1688 const DecoderFinalStatus& aStatus, const ImageMetadata& aMetadata,
1689 const DecoderTelemetry& aTelemetry, Progress aProgress,
1690 const UnorientedIntRect& aInvalidRect, const Maybe<uint32_t>& aFrameCount,
1691 DecoderFlags aDecoderFlags, SurfaceFlags aSurfaceFlags) {
1692 MOZ_ASSERT(NS_IsMainThread());
1694 // If the decoder detected an error, log it to the error console.
1695 if (aStatus.mShouldReportError) {
1696 ReportDecoderError();
1699 // Record all the metadata the decoder gathered about this image.
1700 bool metadataOK = SetMetadata(aMetadata, aStatus.mWasMetadataDecode);
1701 if (!metadataOK) {
1702 // This indicates a serious error that requires us to discard all existing
1703 // surfaces and redecode to recover. We'll drop the results from this
1704 // decoder on the floor, since they aren't valid.
1705 RecoverFromInvalidFrames(ToUnoriented(mSize),
1706 FromSurfaceFlags(aSurfaceFlags));
1707 return;
1710 MOZ_ASSERT(mError || LoadHasSize() || !aMetadata.HasSize(),
1711 "SetMetadata should've gotten a size");
1713 if (!aStatus.mWasMetadataDecode && aStatus.mFinished) {
1714 // Flag that we've been decoded before.
1715 StoreHasBeenDecoded(true);
1718 // Send out any final notifications.
1719 NotifyProgress(aProgress, aInvalidRect, aFrameCount, aDecoderFlags,
1720 aSurfaceFlags);
1722 if (!(aDecoderFlags & DecoderFlags::FIRST_FRAME_ONLY)) {
1723 // We may have decoded new animation frames; update our animation state.
1724 MOZ_ASSERT_IF(aFrameCount && *aFrameCount > 1, mAnimationState || mError);
1725 if (mAnimationState && aFrameCount) {
1726 mAnimationState->UpdateKnownFrameCount(*aFrameCount);
1729 // If we should start animating right now, do so.
1730 if (mAnimationState && aFrameCount == Some(1u) && LoadPendingAnimation() &&
1731 ShouldAnimate()) {
1732 StartAnimation();
1735 if (mAnimationState && LoadHasBeenDecoded()) {
1736 // We've finished a full decode of all animation frames and our
1737 // AnimationState has been notified about them all, so let it know not to
1738 // expect anymore.
1739 mAnimationState->NotifyDecodeComplete();
1741 auto size = ToUnoriented(mSize);
1742 IntRect rect = mAnimationState->UpdateState(this, size.ToUnknownSize());
1744 if (!rect.IsEmpty()) {
1745 auto dirtyRect = UnorientedIntRect::FromUnknownRect(rect);
1746 NotifyProgress(NoProgress, dirtyRect);
1751 // Do some telemetry if this isn't a metadata decode.
1752 if (!aStatus.mWasMetadataDecode) {
1753 if (aTelemetry.mChunkCount) {
1754 Telemetry::Accumulate(Telemetry::IMAGE_DECODE_CHUNKS,
1755 aTelemetry.mChunkCount);
1758 if (aStatus.mFinished) {
1759 Telemetry::Accumulate(Telemetry::IMAGE_DECODE_TIME,
1760 int32_t(aTelemetry.mDecodeTime.ToMicroseconds()));
1762 if (aTelemetry.mSpeedHistogram && aTelemetry.mBytesDecoded) {
1763 Telemetry::Accumulate(*aTelemetry.mSpeedHistogram, aTelemetry.Speed());
1768 // Only act on errors if we have no usable frames from the decoder.
1769 if (aStatus.mHadError &&
1770 (!mAnimationState || mAnimationState->KnownFrameCount() == 0)) {
1771 DoError();
1772 } else if (aStatus.mWasMetadataDecode && !LoadHasSize()) {
1773 DoError();
1776 // XXX(aosmond): Can we get this far without mFinished == true?
1777 if (aStatus.mFinished && aStatus.mWasMetadataDecode) {
1778 // If we were waiting to fire the load event, go ahead and fire it now.
1779 if (mLoadProgress) {
1780 NotifyForLoadEvent(*mLoadProgress);
1781 mLoadProgress = Nothing();
1784 // If we were a metadata decode and a full decode was requested, do it.
1785 if (LoadWantFullDecode()) {
1786 StoreWantFullDecode(false);
1787 RequestDecodeForSize(mSize.ToUnknownSize(),
1788 DECODE_FLAGS_DEFAULT | FLAG_HIGH_QUALITY_SCALING,
1789 FRAME_CURRENT);
1794 void RasterImage::ReportDecoderError() {
1795 nsCOMPtr<nsIConsoleService> consoleService =
1796 do_GetService(NS_CONSOLESERVICE_CONTRACTID);
1797 nsCOMPtr<nsIScriptError> errorObject =
1798 do_CreateInstance(NS_SCRIPTERROR_CONTRACTID);
1800 if (consoleService && errorObject) {
1801 nsAutoString msg(u"Image corrupt or truncated."_ns);
1802 nsAutoString src;
1803 if (GetURI()) {
1804 nsAutoCString uri;
1805 if (!GetSpecTruncatedTo1k(uri)) {
1806 msg += u" URI in this note truncated due to length."_ns;
1808 CopyUTF8toUTF16(uri, src);
1810 if (NS_SUCCEEDED(errorObject->InitWithWindowID(msg, src, u""_ns, 0, 0,
1811 nsIScriptError::errorFlag,
1812 "Image", InnerWindowID()))) {
1813 consoleService->LogMessage(errorObject);
1818 already_AddRefed<imgIContainer> RasterImage::Unwrap() {
1819 nsCOMPtr<imgIContainer> self(this);
1820 return self.forget();
1823 void RasterImage::PropagateUseCounters(dom::Document*) {
1824 // No use counters.
1827 IntSize RasterImage::OptimalImageSizeForDest(const gfxSize& aDest,
1828 uint32_t aWhichFrame,
1829 SamplingFilter aSamplingFilter,
1830 uint32_t aFlags) {
1831 MOZ_ASSERT(aDest.width >= 0 || ceil(aDest.width) <= INT32_MAX ||
1832 aDest.height >= 0 || ceil(aDest.height) <= INT32_MAX,
1833 "Unexpected destination size");
1835 if (mSize.IsEmpty() || aDest.IsEmpty()) {
1836 return IntSize(0, 0);
1839 auto dest = OrientedIntSize::FromUnknownSize(
1840 IntSize::Ceil(aDest.width, aDest.height));
1842 if (aSamplingFilter == SamplingFilter::GOOD &&
1843 CanDownscaleDuringDecode(ToUnoriented(dest), aFlags)) {
1844 return dest.ToUnknownSize();
1847 // We can't scale to this size. Use our intrinsic size for now.
1848 return mSize.ToUnknownSize();
1851 gfxMatrix RasterImage::OrientationMatrix(const UnorientedIntSize& aSize,
1852 bool aInvert) const {
1853 return OrientedImage::OrientationMatrix(UsedOrientation(),
1854 aSize.ToUnknownSize(), aInvert);
1858 * Rotate aRect by the given angle within the space specified by aSize.
1860 * For example, with aRect = [20, 10, 5, 5] and aSize = [100, 100], rotating
1861 * with Angle::D90 will result in aRect = [85, 20, 5, 5].
1863 static void Rotate(IntRect& aRect, const IntSize& aSize, Angle aAngle) {
1864 switch (aAngle) {
1865 case Angle::D0:
1866 break;
1867 case Angle::D90:
1868 aRect = {aSize.height - aRect.YMost(), aRect.x, aRect.height,
1869 aRect.width};
1870 break;
1871 case Angle::D180:
1872 aRect.MoveTo(aSize.width - aRect.XMost(), aSize.height - aRect.YMost());
1873 break;
1874 case Angle::D270:
1875 aRect = {aRect.y, aSize.width - aRect.XMost(), aRect.height, aRect.width};
1876 break;
1881 * Flip aRect along the central axis within aSize.
1883 * For example, with aRect = [20, 10, 5, 5] and aSize = [100, 100], flipping
1884 * with Flip::Horizontal will result in aRect = [75, 10, 5, 5].
1886 static void Flip(IntRect& aRect, const IntSize& aSize, Flip aFlip) {
1887 switch (aFlip) {
1888 case Flip::Unflipped:
1889 break;
1890 case Flip::Horizontal:
1891 aRect.x = aSize.width - aRect.XMost();
1892 break;
1896 OrientedIntRect RasterImage::ToOriented(UnorientedIntRect aRect) const {
1897 IntRect rect = aRect.ToUnknownRect();
1898 auto size = ToUnoriented(mSize);
1900 MOZ_ASSERT(!UsedOrientation().flipFirst,
1901 "flipFirst should only be used by OrientedImage");
1903 // UsedOrientation() specifies the transformation from a correctly oriented
1904 // image to the pixels stored in the file, so we need to rotate by the
1905 // negation of the given angle.
1906 Angle angle = Orientation::InvertAngle(UsedOrientation().rotation);
1907 Rotate(rect, size.ToUnknownSize(), angle);
1909 // Use mSize instead of size, since after the Rotate call, the size of the
1910 // space that rect is in has had its width and height swapped.
1911 Flip(rect, mSize.ToUnknownSize(), UsedOrientation().flip);
1913 return OrientedIntRect::FromUnknownRect(rect);
1916 UnorientedIntRect RasterImage::ToUnoriented(OrientedIntRect aRect) const {
1917 IntRect rect = aRect.ToUnknownRect();
1919 Flip(rect, mSize.ToUnknownSize(), UsedOrientation().flip);
1920 Rotate(rect, mSize.ToUnknownSize(), UsedOrientation().rotation);
1922 MOZ_ASSERT(!UsedOrientation().flipFirst,
1923 "flipFirst should only be used by OrientedImage");
1925 return UnorientedIntRect::FromUnknownRect(rect);
1928 } // namespace image
1929 } // namespace mozilla