Bug 1577199 test speaker-selection permissions policy on selectAudioOutput() r=jib
[gecko.git] / image / RasterImage.cpp
blobf7963f9f7b070a7c34433c4e22f01645a1461717
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*>()) {
83 //******************************************************************************
84 RasterImage::~RasterImage() {
85 // Make sure our SourceBuffer is marked as complete. This will ensure that any
86 // outstanding decoders terminate.
87 if (!mSourceBuffer->IsComplete()) {
88 mSourceBuffer->Complete(NS_ERROR_ABORT);
91 // Release all frames from the surface cache.
92 SurfaceCache::RemoveImage(ImageKey(this));
94 // Record Telemetry.
95 Telemetry::Accumulate(Telemetry::IMAGE_DECODE_COUNT, mDecodeCount);
98 nsresult RasterImage::Init(const char* aMimeType, uint32_t aFlags) {
99 // We don't support re-initialization
100 if (mInitialized) {
101 return NS_ERROR_ILLEGAL_VALUE;
104 // Not sure an error can happen before init, but be safe
105 if (mError) {
106 return NS_ERROR_FAILURE;
109 // We want to avoid redecodes for transient images.
110 MOZ_ASSERT_IF(aFlags & INIT_FLAG_TRANSIENT,
111 !(aFlags & INIT_FLAG_DISCARDABLE));
113 // Store initialization data
114 StoreDiscardable(!!(aFlags & INIT_FLAG_DISCARDABLE));
115 StoreWantFullDecode(!!(aFlags & INIT_FLAG_DECODE_IMMEDIATELY));
116 StoreTransient(!!(aFlags & INIT_FLAG_TRANSIENT));
117 StoreSyncLoad(!!(aFlags & INIT_FLAG_SYNC_LOAD));
119 // Use the MIME type to select a decoder type, and make sure there *is* a
120 // decoder for this MIME type.
121 NS_ENSURE_ARG_POINTER(aMimeType);
122 mDecoderType = DecoderFactory::GetDecoderType(aMimeType);
123 if (mDecoderType == DecoderType::UNKNOWN) {
124 return NS_ERROR_FAILURE;
127 // Lock this image's surfaces in the SurfaceCache if we're not discardable.
128 if (!LoadDiscardable()) {
129 mLockCount++;
130 SurfaceCache::LockImage(ImageKey(this));
133 // Mark us as initialized
134 mInitialized = true;
136 return NS_OK;
139 //******************************************************************************
140 NS_IMETHODIMP_(void)
141 RasterImage::RequestRefresh(const TimeStamp& aTime) {
142 if (HadRecentRefresh(aTime)) {
143 return;
146 EvaluateAnimation();
148 if (!mAnimating) {
149 return;
152 RefreshResult res;
153 if (mAnimationState) {
154 MOZ_ASSERT(mFrameAnimator);
155 res = mFrameAnimator->RequestRefresh(*mAnimationState, aTime);
158 #ifdef DEBUG
159 if (res.mFrameAdvanced) {
160 mFramesNotified++;
162 #endif
164 // Notify listeners that our frame has actually changed, but do this only
165 // once for all frames that we've now passed (if AdvanceFrame() was called
166 // more than once).
167 if (!res.mDirtyRect.IsEmpty() || res.mFrameAdvanced) {
168 auto dirtyRect = UnorientedIntRect::FromUnknownRect(res.mDirtyRect);
169 NotifyProgress(NoProgress, dirtyRect);
172 if (res.mAnimationFinished) {
173 StoreAnimationFinished(true);
174 EvaluateAnimation();
178 //******************************************************************************
179 NS_IMETHODIMP
180 RasterImage::GetWidth(int32_t* aWidth) {
181 NS_ENSURE_ARG_POINTER(aWidth);
183 if (mError) {
184 *aWidth = 0;
185 return NS_ERROR_FAILURE;
188 *aWidth = mSize.width;
189 return NS_OK;
192 //******************************************************************************
193 NS_IMETHODIMP
194 RasterImage::GetHeight(int32_t* aHeight) {
195 NS_ENSURE_ARG_POINTER(aHeight);
197 if (mError) {
198 *aHeight = 0;
199 return NS_ERROR_FAILURE;
202 *aHeight = mSize.height;
203 return NS_OK;
206 //******************************************************************************
207 nsresult RasterImage::GetNativeSizes(nsTArray<IntSize>& aNativeSizes) const {
208 if (mError) {
209 return NS_ERROR_FAILURE;
212 aNativeSizes.Clear();
214 if (mNativeSizes.IsEmpty()) {
215 aNativeSizes.AppendElement(mSize.ToUnknownSize());
216 } else {
217 for (const auto& size : mNativeSizes) {
218 aNativeSizes.AppendElement(size.ToUnknownSize());
222 return NS_OK;
225 //******************************************************************************
226 size_t RasterImage::GetNativeSizesLength() const {
227 if (mError || !LoadHasSize()) {
228 return 0;
231 if (mNativeSizes.IsEmpty()) {
232 return 1;
235 return mNativeSizes.Length();
238 //******************************************************************************
239 NS_IMETHODIMP
240 RasterImage::GetIntrinsicSize(nsSize* aSize) {
241 if (mError) {
242 return NS_ERROR_FAILURE;
245 *aSize = nsSize(nsPresContext::CSSPixelsToAppUnits(mSize.width),
246 nsPresContext::CSSPixelsToAppUnits(mSize.height));
247 return NS_OK;
250 //******************************************************************************
251 Maybe<AspectRatio> RasterImage::GetIntrinsicRatio() {
252 if (mError) {
253 return Nothing();
256 return Some(AspectRatio::FromSize(mSize.width, mSize.height));
259 NS_IMETHODIMP_(Orientation)
260 RasterImage::GetOrientation() { return mOrientation; }
262 NS_IMETHODIMP_(Resolution)
263 RasterImage::GetResolution() { return mResolution; }
265 //******************************************************************************
266 NS_IMETHODIMP
267 RasterImage::GetType(uint16_t* aType) {
268 NS_ENSURE_ARG_POINTER(aType);
270 *aType = imgIContainer::TYPE_RASTER;
271 return NS_OK;
274 NS_IMETHODIMP
275 RasterImage::GetProducerId(uint32_t* aId) {
276 NS_ENSURE_ARG_POINTER(aId);
278 *aId = ImageResource::GetImageProducerId();
279 return NS_OK;
282 LookupResult RasterImage::LookupFrameInternal(const UnorientedIntSize& aSize,
283 uint32_t aFlags,
284 PlaybackType aPlaybackType,
285 bool aMarkUsed) {
286 if (mAnimationState && aPlaybackType == PlaybackType::eAnimated) {
287 MOZ_ASSERT(mFrameAnimator);
288 MOZ_ASSERT(ToSurfaceFlags(aFlags) == DefaultSurfaceFlags(),
289 "Can't composite frames with non-default surface flags");
290 return mFrameAnimator->GetCompositedFrame(*mAnimationState, aMarkUsed);
293 SurfaceFlags surfaceFlags = ToSurfaceFlags(aFlags);
295 // We don't want any substitution for sync decodes, and substitution would be
296 // illegal when high quality downscaling is disabled, so we use
297 // SurfaceCache::Lookup in this case.
298 if ((aFlags & FLAG_SYNC_DECODE) || !(aFlags & FLAG_HIGH_QUALITY_SCALING)) {
299 return SurfaceCache::Lookup(
300 ImageKey(this),
301 RasterSurfaceKey(aSize.ToUnknownSize(), surfaceFlags,
302 PlaybackType::eStatic),
303 aMarkUsed);
306 // We'll return the best match we can find to the requested frame.
307 return SurfaceCache::LookupBestMatch(
308 ImageKey(this),
309 RasterSurfaceKey(aSize.ToUnknownSize(), surfaceFlags,
310 PlaybackType::eStatic),
311 aMarkUsed);
314 LookupResult RasterImage::LookupFrame(const UnorientedIntSize& aSize,
315 uint32_t aFlags,
316 PlaybackType aPlaybackType,
317 bool aMarkUsed) {
318 MOZ_ASSERT(NS_IsMainThread());
320 // If we're opaque, we don't need to care about premultiplied alpha, because
321 // that can only matter for frames with transparency.
322 if (IsOpaque()) {
323 aFlags &= ~FLAG_DECODE_NO_PREMULTIPLY_ALPHA;
326 UnorientedIntSize requestedSize =
327 CanDownscaleDuringDecode(aSize, aFlags) ? aSize : ToUnoriented(mSize);
328 if (requestedSize.IsEmpty()) {
329 // Can't decode to a surface of zero size.
330 return LookupResult(MatchType::NOT_FOUND);
333 LookupResult result =
334 LookupFrameInternal(requestedSize, aFlags, aPlaybackType, aMarkUsed);
336 if (!result && !LoadHasSize()) {
337 // We can't request a decode without knowing our intrinsic size. Give up.
338 return LookupResult(MatchType::NOT_FOUND);
341 const bool syncDecode = aFlags & FLAG_SYNC_DECODE;
342 const bool avoidRedecode = aFlags & FLAG_AVOID_REDECODE_FOR_SIZE;
343 if (result.Type() == MatchType::NOT_FOUND ||
344 (result.Type() == MatchType::SUBSTITUTE_BECAUSE_NOT_FOUND &&
345 !avoidRedecode) ||
346 (syncDecode && !avoidRedecode && !result)) {
347 // We don't have a copy of this frame, and there's no decoder working on
348 // one. (Or we're sync decoding and the existing decoder hasn't even started
349 // yet.) Trigger decoding so it'll be available next time.
350 MOZ_ASSERT(aPlaybackType != PlaybackType::eAnimated ||
351 StaticPrefs::image_mem_animated_discardable_AtStartup() ||
352 !mAnimationState || mAnimationState->KnownFrameCount() < 1,
353 "Animated frames should be locked");
355 // The surface cache may suggest the preferred size we are supposed to
356 // decode at. This should only happen if we accept substitutions.
357 if (!result.SuggestedSize().IsEmpty()) {
358 MOZ_ASSERT(!syncDecode && (aFlags & FLAG_HIGH_QUALITY_SCALING));
359 requestedSize =
360 UnorientedIntSize::FromUnknownSize(result.SuggestedSize());
363 bool ranSync = false, failed = false;
364 Decode(requestedSize, aFlags, aPlaybackType, ranSync, failed);
365 if (failed) {
366 result.SetFailedToRequestDecode();
369 // If we can or did sync decode, we should already have the frame.
370 if (ranSync || syncDecode) {
371 result =
372 LookupFrameInternal(requestedSize, aFlags, aPlaybackType, aMarkUsed);
376 if (!result) {
377 // We still weren't able to get a frame. Give up.
378 return result;
381 // Sync decoding guarantees that we got the frame, but if it's owned by an
382 // async decoder that's currently running, the contents of the frame may not
383 // be available yet. Make sure we get everything.
384 if (LoadAllSourceData() && syncDecode) {
385 result.Surface()->WaitUntilFinished();
388 // If we could have done some decoding in this function we need to check if
389 // that decoding encountered an error and hence aborted the surface. We want
390 // to avoid calling IsAborted if we weren't passed any sync decode flag
391 // because IsAborted acquires the monitor for the imgFrame.
392 if (aFlags & (FLAG_SYNC_DECODE | FLAG_SYNC_DECODE_IF_FAST) &&
393 result.Surface()->IsAborted()) {
394 DrawableSurface tmp = std::move(result.Surface());
395 return result;
398 return result;
401 bool RasterImage::IsOpaque() {
402 if (mError) {
403 return false;
406 Progress progress = mProgressTracker->GetProgress();
408 // If we haven't yet finished decoding, the safe answer is "not opaque".
409 if (!(progress & FLAG_DECODE_COMPLETE)) {
410 return false;
413 // Other, we're opaque if FLAG_HAS_TRANSPARENCY is not set.
414 return !(progress & FLAG_HAS_TRANSPARENCY);
417 NS_IMETHODIMP_(bool)
418 RasterImage::WillDrawOpaqueNow() {
419 if (!IsOpaque()) {
420 return false;
423 if (mAnimationState) {
424 if (!StaticPrefs::image_mem_animated_discardable_AtStartup()) {
425 // We never discard frames of animated images.
426 return true;
427 } else {
428 if (mAnimationState->GetCompositedFrameInvalid()) {
429 // We're not going to draw anything at all.
430 return false;
435 // If we are not locked our decoded data could get discard at any time (ie
436 // between the call to this function and when we are asked to draw), so we
437 // have to return false if we are unlocked.
438 if (mLockCount == 0) {
439 return false;
442 auto size = ToUnoriented(mSize);
443 LookupResult result = SurfaceCache::LookupBestMatch(
444 ImageKey(this),
445 RasterSurfaceKey(size.ToUnknownSize(), DefaultSurfaceFlags(),
446 PlaybackType::eStatic),
447 /* aMarkUsed = */ false);
448 MatchType matchType = result.Type();
449 if (matchType == MatchType::NOT_FOUND || matchType == MatchType::PENDING ||
450 !result.Surface()->IsFinished()) {
451 return false;
454 return true;
457 void RasterImage::OnSurfaceDiscarded(const SurfaceKey& aSurfaceKey) {
458 MOZ_ASSERT(mProgressTracker);
460 bool animatedFramesDiscarded =
461 mAnimationState && aSurfaceKey.Playback() == PlaybackType::eAnimated;
463 nsCOMPtr<nsIEventTarget> eventTarget;
464 if (mProgressTracker) {
465 eventTarget = mProgressTracker->GetEventTarget();
466 } else {
467 eventTarget = do_GetMainThread();
470 RefPtr<RasterImage> image = this;
471 nsCOMPtr<nsIRunnable> ev =
472 NS_NewRunnableFunction("RasterImage::OnSurfaceDiscarded", [=]() -> void {
473 image->OnSurfaceDiscardedInternal(animatedFramesDiscarded);
475 eventTarget->Dispatch(ev.forget(), NS_DISPATCH_NORMAL);
478 void RasterImage::OnSurfaceDiscardedInternal(bool aAnimatedFramesDiscarded) {
479 MOZ_ASSERT(NS_IsMainThread());
481 if (aAnimatedFramesDiscarded && mAnimationState) {
482 MOZ_ASSERT(StaticPrefs::image_mem_animated_discardable_AtStartup());
483 ReleaseImageContainer();
485 auto size = ToUnoriented(mSize);
486 IntRect rect = mAnimationState->UpdateState(this, size.ToUnknownSize());
488 auto dirtyRect = UnorientedIntRect::FromUnknownRect(rect);
489 NotifyProgress(NoProgress, dirtyRect);
492 if (mProgressTracker) {
493 mProgressTracker->OnDiscard();
497 //******************************************************************************
498 NS_IMETHODIMP
499 RasterImage::GetAnimated(bool* aAnimated) {
500 if (mError) {
501 return NS_ERROR_FAILURE;
504 NS_ENSURE_ARG_POINTER(aAnimated);
506 // If we have an AnimationState, we can know for sure.
507 if (mAnimationState) {
508 *aAnimated = true;
509 return NS_OK;
512 // Otherwise, we need to have been decoded to know for sure, since if we were
513 // decoded at least once mAnimationState would have been created for animated
514 // images. This is true even though we check for animation during the
515 // metadata decode, because we may still discover animation only during the
516 // full decode for corrupt images.
517 if (!LoadHasBeenDecoded()) {
518 return NS_ERROR_NOT_AVAILABLE;
521 // We know for sure
522 *aAnimated = false;
524 return NS_OK;
527 //******************************************************************************
528 NS_IMETHODIMP_(int32_t)
529 RasterImage::GetFirstFrameDelay() {
530 if (mError) {
531 return -1;
534 bool animated = false;
535 if (NS_FAILED(GetAnimated(&animated)) || !animated) {
536 return -1;
539 MOZ_ASSERT(mAnimationState, "Animated images should have an AnimationState");
540 return mAnimationState->FirstFrameTimeout().AsEncodedValueDeprecated();
543 NS_IMETHODIMP_(already_AddRefed<SourceSurface>)
544 RasterImage::GetFrame(uint32_t aWhichFrame, uint32_t aFlags) {
545 return GetFrameAtSize(mSize.ToUnknownSize(), aWhichFrame, aFlags);
548 NS_IMETHODIMP_(already_AddRefed<SourceSurface>)
549 RasterImage::GetFrameAtSize(const IntSize& aSize, uint32_t aWhichFrame,
550 uint32_t aFlags) {
551 AutoProfilerImagePaintMarker PROFILER_RAII(this);
552 #ifdef DEBUG
553 NotifyDrawingObservers();
554 #endif
556 auto result =
557 GetFrameInternal(aSize, Nothing(), Nothing(), aWhichFrame, aFlags);
558 return mozilla::Get<2>(result).forget();
561 Tuple<ImgDrawResult, IntSize, RefPtr<SourceSurface>>
562 RasterImage::GetFrameInternal(const IntSize& aSize,
563 const Maybe<SVGImageContext>& aSVGContext,
564 const Maybe<ImageIntRegion>& aRegion,
565 uint32_t aWhichFrame, uint32_t aFlags) {
566 MOZ_ASSERT(aWhichFrame <= FRAME_MAX_VALUE);
568 auto size = OrientedIntSize::FromUnknownSize(aSize);
570 if (aSize.IsEmpty() || aWhichFrame > FRAME_MAX_VALUE) {
571 return MakeTuple(ImgDrawResult::BAD_ARGS, aSize, RefPtr<SourceSurface>());
574 if (mError) {
575 return MakeTuple(ImgDrawResult::BAD_IMAGE, aSize, RefPtr<SourceSurface>());
578 // Get the frame. If it's not there, it's probably the caller's fault for
579 // not waiting for the data to be loaded from the network or not passing
580 // FLAG_SYNC_DECODE.
581 LookupResult result =
582 LookupFrame(ToUnoriented(size), aFlags, ToPlaybackType(aWhichFrame),
583 /* aMarkUsed = */ true);
584 auto resultSuggestedSize =
585 UnorientedIntSize::FromUnknownSize(result.SuggestedSize());
587 // The surface cache may have suggested we use a different size than the
588 // given size in the future. This may or may not be accompanied by an
589 // actual surface, depending on what it has in its cache.
590 OrientedIntSize suggestedSize = ToOriented(resultSuggestedSize);
591 if (suggestedSize.IsEmpty()) {
592 suggestedSize = size;
594 MOZ_ASSERT_IF(result.Type() == MatchType::SUBSTITUTE_BECAUSE_BEST,
595 suggestedSize != size);
597 if (!result) {
598 // The OS threw this frame away and we couldn't redecode it.
599 return MakeTuple(ImgDrawResult::TEMPORARY_ERROR,
600 suggestedSize.ToUnknownSize(), RefPtr<SourceSurface>());
603 RefPtr<SourceSurface> surface = result.Surface()->GetSourceSurface();
605 // If this RasterImage requires orientation, we must return a newly created
606 // surface with the oriented image instead of returning the frame's surface
607 // directly.
608 surface = OrientedImage::OrientSurface(mOrientation, surface);
610 if (!result.Surface()->IsFinished()) {
611 return MakeTuple(ImgDrawResult::INCOMPLETE, suggestedSize.ToUnknownSize(),
612 std::move(surface));
615 return MakeTuple(ImgDrawResult::SUCCESS, suggestedSize.ToUnknownSize(),
616 std::move(surface));
619 Tuple<ImgDrawResult, IntSize> RasterImage::GetImageContainerSize(
620 LayerManager* aManager, const IntSize& aRequestedSize, uint32_t aFlags) {
621 if (!LoadHasSize()) {
622 return MakeTuple(ImgDrawResult::NOT_READY, IntSize(0, 0));
625 if (aRequestedSize.IsEmpty()) {
626 return MakeTuple(ImgDrawResult::BAD_ARGS, IntSize(0, 0));
629 // We check the minimum size because while we support downscaling, we do not
630 // support upscaling. If aRequestedSize > mSize, we will never give a larger
631 // surface than mSize. If mSize > aRequestedSize, and mSize > maxTextureSize,
632 // we still want to use image containers if aRequestedSize <= maxTextureSize.
633 int32_t maxTextureSize = aManager->GetMaxTextureSize();
634 if (min(mSize.width, aRequestedSize.width) > maxTextureSize ||
635 min(mSize.height, aRequestedSize.height) > maxTextureSize) {
636 return MakeTuple(ImgDrawResult::NOT_SUPPORTED, IntSize(0, 0));
639 auto requestedSize = OrientedIntSize::FromUnknownSize(aRequestedSize);
640 if (!CanDownscaleDuringDecode(ToUnoriented(requestedSize), aFlags)) {
641 return MakeTuple(ImgDrawResult::SUCCESS, mSize.ToUnknownSize());
644 return MakeTuple(ImgDrawResult::SUCCESS, aRequestedSize);
647 NS_IMETHODIMP_(bool)
648 RasterImage::IsImageContainerAvailable(LayerManager* aManager,
649 uint32_t aFlags) {
650 return IsImageContainerAvailableAtSize(aManager, mSize.ToUnknownSize(),
651 aFlags);
654 NS_IMETHODIMP_(already_AddRefed<ImageContainer>)
655 RasterImage::GetImageContainer(LayerManager* aManager, uint32_t aFlags) {
656 // Strip out unsupported flags for raster images.
657 uint32_t flags = aFlags & ~(FLAG_RECORD_BLOB);
659 RefPtr<ImageContainer> container;
660 ImgDrawResult drawResult =
661 GetImageContainerImpl(aManager, mSize.ToUnknownSize(), Nothing(),
662 Nothing(), flags, getter_AddRefs(container));
664 // We silence the unused warning here because anything that needs the draw
665 // result should be using GetImageContainerAtSize, not GetImageContainer.
666 (void)drawResult;
667 return container.forget();
670 NS_IMETHODIMP_(bool)
671 RasterImage::IsImageContainerAvailableAtSize(LayerManager* aManager,
672 const IntSize& aRequestedSize,
673 uint32_t aFlags) {
674 // We check the minimum size because while we support downscaling, we do not
675 // support upscaling. If aRequestedSize > mSize, we will never give a larger
676 // surface than mSize. If mSize > aRequestedSize, and mSize > maxTextureSize,
677 // we still want to use image containers if aRequestedSize <= maxTextureSize.
678 int32_t maxTextureSize = aManager->GetMaxTextureSize();
679 if (!LoadHasSize() || aRequestedSize.IsEmpty() ||
680 min(mSize.width, aRequestedSize.width) > maxTextureSize ||
681 min(mSize.height, aRequestedSize.height) > maxTextureSize) {
682 return false;
685 return true;
688 NS_IMETHODIMP_(ImgDrawResult)
689 RasterImage::GetImageContainerAtSize(layers::LayerManager* aManager,
690 const gfx::IntSize& aSize,
691 const Maybe<SVGImageContext>& aSVGContext,
692 const Maybe<ImageIntRegion>& aRegion,
693 uint32_t aFlags,
694 layers::ImageContainer** aOutContainer) {
695 // We do not pass in the given SVG context because in theory it could differ
696 // between calls, but actually have no impact on the actual contents of the
697 // image container.
698 return GetImageContainerImpl(aManager, aSize, Nothing(), Nothing(), aFlags,
699 aOutContainer);
702 size_t RasterImage::SizeOfSourceWithComputedFallback(
703 SizeOfState& aState) const {
704 return mSourceBuffer->SizeOfIncludingThisWithComputedFallback(
705 aState.mMallocSizeOf);
708 void RasterImage::CollectSizeOfSurfaces(
709 nsTArray<SurfaceMemoryCounter>& aCounters,
710 MallocSizeOf aMallocSizeOf) const {
711 SurfaceCache::CollectSizeOfSurfaces(ImageKey(this), aCounters, aMallocSizeOf);
712 ImageResource::CollectSizeOfSurfaces(aCounters, aMallocSizeOf);
715 bool RasterImage::SetMetadata(const ImageMetadata& aMetadata,
716 bool aFromMetadataDecode) {
717 MOZ_ASSERT(NS_IsMainThread());
719 if (mError) {
720 return true;
723 mResolution = aMetadata.GetResolution();
725 if (aMetadata.HasSize()) {
726 auto metadataSize = UnorientedIntSize::FromUnknownSize(aMetadata.GetSize());
727 if (metadataSize.width < 0 || metadataSize.height < 0) {
728 NS_WARNING("Image has negative intrinsic size");
729 DoError();
730 return true;
733 MOZ_ASSERT(aMetadata.HasOrientation());
734 Orientation orientation = aMetadata.GetOrientation();
736 // If we already have a size, check the new size against the old one.
737 if (LoadHasSize() &&
738 (metadataSize != ToUnoriented(mSize) || orientation != mOrientation)) {
739 NS_WARNING(
740 "Image changed size or orientation on redecode! "
741 "This should not happen!");
742 DoError();
743 return true;
746 // Set the size and flag that we have it.
747 mOrientation = orientation;
748 mSize = ToOriented(metadataSize);
749 mNativeSizes.Clear();
750 for (const auto& nativeSize : aMetadata.GetNativeSizes()) {
751 mNativeSizes.AppendElement(
752 ToOriented(UnorientedIntSize::FromUnknownSize(nativeSize)));
754 StoreHasSize(true);
757 if (LoadHasSize() && aMetadata.HasAnimation() && !mAnimationState) {
758 // We're becoming animated, so initialize animation stuff.
759 mAnimationState.emplace(mAnimationMode);
760 mFrameAnimator =
761 MakeUnique<FrameAnimator>(this, ToUnoriented(mSize).ToUnknownSize());
763 if (!StaticPrefs::image_mem_animated_discardable_AtStartup()) {
764 // We don't support discarding animated images (See bug 414259).
765 // Lock the image and throw away the key.
766 LockImage();
769 if (!aFromMetadataDecode) {
770 // The metadata decode reported that this image isn't animated, but we
771 // discovered that it actually was during the full decode. This is a
772 // rare failure that only occurs for corrupt images. To recover, we need
773 // to discard all existing surfaces and redecode.
774 return false;
778 if (mAnimationState) {
779 mAnimationState->SetLoopCount(aMetadata.GetLoopCount());
780 mAnimationState->SetFirstFrameTimeout(aMetadata.GetFirstFrameTimeout());
782 if (aMetadata.HasLoopLength()) {
783 mAnimationState->SetLoopLength(aMetadata.GetLoopLength());
785 if (aMetadata.HasFirstFrameRefreshArea()) {
786 mAnimationState->SetFirstFrameRefreshArea(
787 aMetadata.GetFirstFrameRefreshArea());
791 if (aMetadata.HasHotspot()) {
792 // NOTE(heycam): We shouldn't have any image formats that support both
793 // orientation and hotspots, so we assert that rather than add code
794 // to orient the hotspot point correctly.
795 MOZ_ASSERT(mOrientation.IsIdentity(), "Would need to orient hotspot point");
797 auto hotspot = aMetadata.GetHotspot();
798 mHotspot.x = std::max(std::min(hotspot.x, mSize.width - 1), 0);
799 mHotspot.y = std::max(std::min(hotspot.y, mSize.height - 1), 0);
802 return true;
805 NS_IMETHODIMP
806 RasterImage::SetAnimationMode(uint16_t aAnimationMode) {
807 if (mAnimationState) {
808 mAnimationState->SetAnimationMode(aAnimationMode);
810 return SetAnimationModeInternal(aAnimationMode);
813 //******************************************************************************
815 nsresult RasterImage::StartAnimation() {
816 if (mError) {
817 return NS_ERROR_FAILURE;
820 MOZ_ASSERT(ShouldAnimate(), "Should not animate!");
822 // If we're not ready to animate, then set mPendingAnimation, which will cause
823 // us to start animating if and when we do become ready.
824 StorePendingAnimation(!mAnimationState ||
825 mAnimationState->KnownFrameCount() < 1);
826 if (LoadPendingAnimation()) {
827 return NS_OK;
830 // Don't bother to animate if we're displaying the first frame forever.
831 if (mAnimationState->GetCurrentAnimationFrameIndex() == 0 &&
832 mAnimationState->FirstFrameTimeout() == FrameTimeout::Forever()) {
833 StoreAnimationFinished(true);
834 return NS_ERROR_ABORT;
837 // We need to set the time that this initial frame was first displayed, as
838 // this is used in AdvanceFrame().
839 mAnimationState->InitAnimationFrameTimeIfNecessary();
841 return NS_OK;
844 //******************************************************************************
845 nsresult RasterImage::StopAnimation() {
846 MOZ_ASSERT(mAnimating, "Should be animating!");
848 nsresult rv = NS_OK;
849 if (mError) {
850 rv = NS_ERROR_FAILURE;
851 } else {
852 mAnimationState->SetAnimationFrameTime(TimeStamp());
855 mAnimating = false;
856 return rv;
859 //******************************************************************************
860 NS_IMETHODIMP
861 RasterImage::ResetAnimation() {
862 if (mError) {
863 return NS_ERROR_FAILURE;
866 StorePendingAnimation(false);
868 if (mAnimationMode == kDontAnimMode || !mAnimationState ||
869 mAnimationState->GetCurrentAnimationFrameIndex() == 0) {
870 return NS_OK;
873 StoreAnimationFinished(false);
875 if (mAnimating) {
876 StopAnimation();
879 MOZ_ASSERT(mAnimationState, "Should have AnimationState");
880 MOZ_ASSERT(mFrameAnimator, "Should have FrameAnimator");
881 mFrameAnimator->ResetAnimation(*mAnimationState);
883 IntRect area = mAnimationState->FirstFrameRefreshArea();
884 NotifyProgress(NoProgress, UnorientedIntRect::FromUnknownRect(area));
886 // Start the animation again. It may not have been running before, if
887 // mAnimationFinished was true before entering this function.
888 EvaluateAnimation();
890 return NS_OK;
893 //******************************************************************************
894 NS_IMETHODIMP_(void)
895 RasterImage::SetAnimationStartTime(const TimeStamp& aTime) {
896 if (mError || mAnimationMode == kDontAnimMode || mAnimating ||
897 !mAnimationState) {
898 return;
901 mAnimationState->SetAnimationFrameTime(aTime);
904 NS_IMETHODIMP_(float)
905 RasterImage::GetFrameIndex(uint32_t aWhichFrame) {
906 MOZ_ASSERT(aWhichFrame <= FRAME_MAX_VALUE, "Invalid argument");
907 return (aWhichFrame == FRAME_FIRST || !mAnimationState)
908 ? 0.0f
909 : mAnimationState->GetCurrentAnimationFrameIndex();
912 NS_IMETHODIMP_(IntRect)
913 RasterImage::GetImageSpaceInvalidationRect(const IntRect& aRect) {
914 // Note that we do not transform aRect into an UnorientedIntRect, since
915 // RasterImage::NotifyProgress notifies all consumers of the image using
916 // OrientedIntRect values. (This is unlike OrientedImage, which notifies
917 // using inner image coordinates.)
918 return aRect;
921 nsresult RasterImage::OnImageDataComplete(nsIRequest*, nsISupports*,
922 nsresult aStatus, bool aLastPart) {
923 MOZ_ASSERT(NS_IsMainThread());
925 // Record that we have all the data we're going to get now.
926 StoreAllSourceData(true);
928 // Let decoders know that there won't be any more data coming.
929 mSourceBuffer->Complete(aStatus);
931 // Allow a synchronous metadata decode if mSyncLoad was set, or if we're
932 // running on a single thread (in which case waiting for the async metadata
933 // decoder could delay this image's load event quite a bit), or if this image
934 // is transient.
935 bool canSyncDecodeMetadata =
936 LoadSyncLoad() || LoadTransient() || DecodePool::NumberOfCores() < 2;
938 if (canSyncDecodeMetadata && !LoadHasSize()) {
939 // We're loading this image synchronously, so it needs to be usable after
940 // this call returns. Since we haven't gotten our size yet, we need to do a
941 // synchronous metadata decode here.
942 DecodeMetadata(FLAG_SYNC_DECODE);
945 // Determine our final status, giving precedence to Necko failure codes. We
946 // check after running the metadata decode in case it triggered an error.
947 nsresult finalStatus = mError ? NS_ERROR_FAILURE : NS_OK;
948 if (NS_FAILED(aStatus)) {
949 finalStatus = aStatus;
952 // If loading failed, report an error.
953 if (NS_FAILED(finalStatus)) {
954 DoError();
957 Progress loadProgress = LoadCompleteProgress(aLastPart, mError, finalStatus);
959 if (!LoadHasSize() && !mError) {
960 // We don't have our size yet, so we'll fire the load event in SetSize().
961 MOZ_ASSERT(!canSyncDecodeMetadata,
962 "Firing load async after metadata sync decode?");
963 mLoadProgress = Some(loadProgress);
964 return finalStatus;
967 NotifyForLoadEvent(loadProgress);
969 return finalStatus;
972 void RasterImage::NotifyForLoadEvent(Progress aProgress) {
973 MOZ_ASSERT(LoadHasSize() || mError,
974 "Need to know size before firing load event");
975 MOZ_ASSERT(
976 !LoadHasSize() || (mProgressTracker->GetProgress() & FLAG_SIZE_AVAILABLE),
977 "Should have notified that the size is available if we have it");
979 // If we encountered an error, make sure we notify for that as well.
980 if (mError) {
981 aProgress |= FLAG_HAS_ERROR;
984 // Notify our listeners, which will fire this image's load event.
985 NotifyProgress(aProgress);
988 nsresult RasterImage::OnImageDataAvailable(nsIRequest*, nsISupports*,
989 nsIInputStream* aInputStream,
990 uint64_t, uint32_t aCount) {
991 nsresult rv = mSourceBuffer->AppendFromInputStream(aInputStream, aCount);
992 if (NS_SUCCEEDED(rv) && !LoadSomeSourceData()) {
993 StoreSomeSourceData(true);
994 if (!LoadSyncLoad()) {
995 // Create an async metadata decoder and verify we succeed in doing so.
996 rv = DecodeMetadata(DECODE_FLAGS_DEFAULT);
1000 if (NS_FAILED(rv)) {
1001 DoError();
1003 return rv;
1006 nsresult RasterImage::SetSourceSizeHint(uint32_t aSizeHint) {
1007 if (aSizeHint == 0) {
1008 return NS_OK;
1011 nsresult rv = mSourceBuffer->ExpectLength(aSizeHint);
1012 if (rv == NS_ERROR_OUT_OF_MEMORY) {
1013 // Flush memory, try to get some back, and try again.
1014 rv = nsMemory::HeapMinimize(true);
1015 if (NS_SUCCEEDED(rv)) {
1016 rv = mSourceBuffer->ExpectLength(aSizeHint);
1020 return rv;
1023 nsresult RasterImage::GetHotspotX(int32_t* aX) {
1024 *aX = mHotspot.x;
1025 return NS_OK;
1028 nsresult RasterImage::GetHotspotY(int32_t* aY) {
1029 *aY = mHotspot.y;
1030 return NS_OK;
1033 void RasterImage::Discard() {
1034 MOZ_ASSERT(NS_IsMainThread());
1035 MOZ_ASSERT(CanDiscard(), "Asked to discard but can't");
1036 MOZ_ASSERT(!mAnimationState ||
1037 StaticPrefs::image_mem_animated_discardable_AtStartup(),
1038 "Asked to discard for animated image");
1040 // Delete all the decoded frames.
1041 SurfaceCache::RemoveImage(ImageKey(this));
1043 if (mAnimationState) {
1044 ReleaseImageContainer();
1046 auto size = ToUnoriented(mSize);
1047 IntRect rect = mAnimationState->UpdateState(this, size.ToUnknownSize());
1049 auto dirtyRect = UnorientedIntRect::FromUnknownRect(rect);
1050 NotifyProgress(NoProgress, dirtyRect);
1053 // Notify that we discarded.
1054 if (mProgressTracker) {
1055 mProgressTracker->OnDiscard();
1059 bool RasterImage::CanDiscard() {
1060 return LoadAllSourceData() &&
1061 // Can discard animated images if the pref is set
1062 (!mAnimationState ||
1063 StaticPrefs::image_mem_animated_discardable_AtStartup());
1066 NS_IMETHODIMP
1067 RasterImage::StartDecoding(uint32_t aFlags, uint32_t aWhichFrame) {
1068 if (mError) {
1069 return NS_ERROR_FAILURE;
1072 if (!LoadHasSize()) {
1073 StoreWantFullDecode(true);
1074 return NS_OK;
1077 uint32_t flags = (aFlags & FLAG_ASYNC_NOTIFY) | FLAG_SYNC_DECODE_IF_FAST |
1078 FLAG_HIGH_QUALITY_SCALING;
1079 return RequestDecodeForSize(mSize.ToUnknownSize(), flags, aWhichFrame);
1082 bool RasterImage::StartDecodingWithResult(uint32_t aFlags,
1083 uint32_t aWhichFrame) {
1084 if (mError) {
1085 return false;
1088 if (!LoadHasSize()) {
1089 StoreWantFullDecode(true);
1090 return false;
1093 uint32_t flags = (aFlags & FLAG_ASYNC_NOTIFY) | FLAG_SYNC_DECODE_IF_FAST |
1094 FLAG_HIGH_QUALITY_SCALING;
1095 LookupResult result =
1096 RequestDecodeForSizeInternal(ToUnoriented(mSize), flags, aWhichFrame);
1097 DrawableSurface surface = std::move(result.Surface());
1098 return surface && surface->IsFinished();
1101 imgIContainer::DecodeResult RasterImage::RequestDecodeWithResult(
1102 uint32_t aFlags, uint32_t aWhichFrame) {
1103 MOZ_ASSERT(NS_IsMainThread());
1105 if (mError) {
1106 return imgIContainer::DECODE_REQUEST_FAILED;
1109 uint32_t flags = aFlags | FLAG_ASYNC_NOTIFY;
1110 LookupResult result =
1111 RequestDecodeForSizeInternal(ToUnoriented(mSize), flags, aWhichFrame);
1112 DrawableSurface surface = std::move(result.Surface());
1113 if (surface && surface->IsFinished()) {
1114 return imgIContainer::DECODE_SURFACE_AVAILABLE;
1116 if (result.GetFailedToRequestDecode()) {
1117 return imgIContainer::DECODE_REQUEST_FAILED;
1119 return imgIContainer::DECODE_REQUESTED;
1122 NS_IMETHODIMP
1123 RasterImage::RequestDecodeForSize(const IntSize& aSize, uint32_t aFlags,
1124 uint32_t aWhichFrame) {
1125 MOZ_ASSERT(NS_IsMainThread());
1127 if (mError) {
1128 return NS_ERROR_FAILURE;
1131 RequestDecodeForSizeInternal(
1132 ToUnoriented(OrientedIntSize::FromUnknownSize(aSize)), aFlags,
1133 aWhichFrame);
1135 return NS_OK;
1138 LookupResult RasterImage::RequestDecodeForSizeInternal(
1139 const UnorientedIntSize& aSize, uint32_t aFlags, uint32_t aWhichFrame) {
1140 MOZ_ASSERT(NS_IsMainThread());
1142 if (aWhichFrame > FRAME_MAX_VALUE) {
1143 return LookupResult(MatchType::NOT_FOUND);
1146 if (mError) {
1147 LookupResult result = LookupResult(MatchType::NOT_FOUND);
1148 result.SetFailedToRequestDecode();
1149 return result;
1152 if (!LoadHasSize()) {
1153 StoreWantFullDecode(true);
1154 return LookupResult(MatchType::NOT_FOUND);
1157 // Decide whether to sync decode images we can decode quickly. Here we are
1158 // explicitly trading off flashing for responsiveness in the case that we're
1159 // redecoding an image (see bug 845147).
1160 bool shouldSyncDecodeIfFast =
1161 !LoadHasBeenDecoded() && (aFlags & FLAG_SYNC_DECODE_IF_FAST);
1163 uint32_t flags =
1164 shouldSyncDecodeIfFast ? aFlags : aFlags & ~FLAG_SYNC_DECODE_IF_FAST;
1166 // Perform a frame lookup, which will implicitly start decoding if needed.
1167 return LookupFrame(aSize, flags, ToPlaybackType(aWhichFrame),
1168 /* aMarkUsed = */ false);
1171 static bool LaunchDecodingTask(IDecodingTask* aTask, RasterImage* aImage,
1172 uint32_t aFlags, bool aHaveSourceData) {
1173 if (aHaveSourceData) {
1174 nsCString uri(aImage->GetURIString());
1176 // If we have all the data, we can sync decode if requested.
1177 if (aFlags & imgIContainer::FLAG_SYNC_DECODE) {
1178 DecodePool::Singleton()->SyncRunIfPossible(aTask, uri);
1179 return true;
1182 if (aFlags & imgIContainer::FLAG_SYNC_DECODE_IF_FAST) {
1183 return DecodePool::Singleton()->SyncRunIfPreferred(aTask, uri);
1187 // Perform an async decode. We also take this path if we don't have all the
1188 // source data yet, since sync decoding is impossible in that situation.
1189 DecodePool::Singleton()->AsyncRun(aTask);
1190 return false;
1193 void RasterImage::Decode(const UnorientedIntSize& aSize, uint32_t aFlags,
1194 PlaybackType aPlaybackType, bool& aOutRanSync,
1195 bool& aOutFailed) {
1196 MOZ_ASSERT(NS_IsMainThread());
1198 if (mError) {
1199 aOutFailed = true;
1200 return;
1203 // If we don't have a size yet, we can't do any other decoding.
1204 if (!LoadHasSize()) {
1205 StoreWantFullDecode(true);
1206 return;
1209 // We're about to decode again, which may mean that some of the previous sizes
1210 // we've decoded at aren't useful anymore. We can allow them to expire from
1211 // the cache by unlocking them here. When the decode finishes, it will send an
1212 // invalidation that will cause all instances of this image to redraw. If this
1213 // image is locked, any surfaces that are still useful will become locked
1214 // again when LookupFrame touches them, and the remainder will eventually
1215 // expire.
1216 SurfaceCache::UnlockEntries(ImageKey(this));
1218 // Determine which flags we need to decode this image with.
1219 DecoderFlags decoderFlags = DefaultDecoderFlags();
1220 if (aFlags & FLAG_ASYNC_NOTIFY) {
1221 decoderFlags |= DecoderFlags::ASYNC_NOTIFY;
1223 if (LoadTransient()) {
1224 decoderFlags |= DecoderFlags::IMAGE_IS_TRANSIENT;
1226 if (LoadHasBeenDecoded()) {
1227 decoderFlags |= DecoderFlags::IS_REDECODE;
1229 if ((aFlags & FLAG_SYNC_DECODE) || !(aFlags & FLAG_HIGH_QUALITY_SCALING)) {
1230 // Used SurfaceCache::Lookup instead of SurfaceCache::LookupBestMatch. That
1231 // means the caller can handle a differently sized surface to be returned
1232 // at any point.
1233 decoderFlags |= DecoderFlags::CANNOT_SUBSTITUTE;
1236 SurfaceFlags surfaceFlags = ToSurfaceFlags(aFlags);
1237 if (IsOpaque()) {
1238 // If there's no transparency, it doesn't matter whether we premultiply
1239 // alpha or not.
1240 surfaceFlags &= ~SurfaceFlags::NO_PREMULTIPLY_ALPHA;
1243 // Create a decoder.
1244 RefPtr<IDecodingTask> task;
1245 nsresult rv;
1246 bool animated = mAnimationState && aPlaybackType == PlaybackType::eAnimated;
1247 if (animated) {
1248 size_t currentFrame = mAnimationState->GetCurrentAnimationFrameIndex();
1249 rv = DecoderFactory::CreateAnimationDecoder(
1250 mDecoderType, WrapNotNull(this), mSourceBuffer,
1251 ToUnoriented(mSize).ToUnknownSize(), decoderFlags, surfaceFlags,
1252 currentFrame, getter_AddRefs(task));
1253 } else {
1254 rv = DecoderFactory::CreateDecoder(
1255 mDecoderType, WrapNotNull(this), mSourceBuffer,
1256 ToUnoriented(mSize).ToUnknownSize(), aSize.ToUnknownSize(),
1257 decoderFlags, surfaceFlags, getter_AddRefs(task));
1260 if (rv == NS_ERROR_ALREADY_INITIALIZED) {
1261 // We raced with an already pending decoder, and it finished before we
1262 // managed to insert the new decoder. Pretend we did a sync call to make
1263 // the caller lookup in the surface cache again.
1264 MOZ_ASSERT(!task);
1265 aOutRanSync = true;
1266 return;
1269 if (animated) {
1270 // We pass false for aAllowInvalidation because we may be asked to use
1271 // async notifications. Any potential invalidation here will be sent when
1272 // RequestRefresh is called, or NotifyDecodeComplete.
1273 #ifdef DEBUG
1274 IntRect rect =
1275 #endif
1276 mAnimationState->UpdateState(this, ToUnoriented(mSize).ToUnknownSize(),
1277 false);
1278 MOZ_ASSERT(rect.IsEmpty());
1281 // Make sure DecoderFactory was able to create a decoder successfully.
1282 if (NS_FAILED(rv)) {
1283 MOZ_ASSERT(!task);
1284 aOutFailed = true;
1285 return;
1288 MOZ_ASSERT(task);
1289 mDecodeCount++;
1291 // We're ready to decode; start the decoder.
1292 aOutRanSync = LaunchDecodingTask(task, this, aFlags, LoadAllSourceData());
1295 NS_IMETHODIMP
1296 RasterImage::DecodeMetadata(uint32_t aFlags) {
1297 if (mError) {
1298 return NS_ERROR_FAILURE;
1301 MOZ_ASSERT(!LoadHasSize(), "Should not do unnecessary metadata decodes");
1303 // Create a decoder.
1304 RefPtr<IDecodingTask> task = DecoderFactory::CreateMetadataDecoder(
1305 mDecoderType, WrapNotNull(this), mSourceBuffer);
1307 // Make sure DecoderFactory was able to create a decoder successfully.
1308 if (!task) {
1309 return NS_ERROR_FAILURE;
1312 // We're ready to decode; start the decoder.
1313 LaunchDecodingTask(task, this, aFlags, LoadAllSourceData());
1314 return NS_OK;
1317 void RasterImage::RecoverFromInvalidFrames(const UnorientedIntSize& aSize,
1318 uint32_t aFlags) {
1319 if (!LoadHasSize()) {
1320 return;
1323 NS_WARNING("A RasterImage's frames became invalid. Attempting to recover...");
1325 // Discard all existing frames, since they're probably all now invalid.
1326 SurfaceCache::RemoveImage(ImageKey(this));
1328 // Relock the image if it's supposed to be locked.
1329 if (mLockCount > 0) {
1330 SurfaceCache::LockImage(ImageKey(this));
1333 bool unused1, unused2;
1335 // Animated images require some special handling, because we normally require
1336 // that they never be discarded.
1337 if (mAnimationState) {
1338 Decode(ToUnoriented(mSize), aFlags | FLAG_SYNC_DECODE,
1339 PlaybackType::eAnimated, unused1, unused2);
1340 ResetAnimation();
1341 return;
1344 // For non-animated images, it's fine to recover using an async decode.
1345 Decode(aSize, aFlags, PlaybackType::eStatic, unused1, unused2);
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() ||
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 AutoProfilerImagePaintMarker PROFILER_RAII(this);
1394 #ifdef DEBUG
1395 NotifyDrawingObservers();
1396 #endif
1398 // By now we may have a frame with the requested size. If not, we need to
1399 // adjust the drawing parameters accordingly.
1400 IntSize finalSize = aSurface->GetSize();
1401 bool couldRedecodeForBetterFrame = false;
1402 if (finalSize != aSize.ToUnknownSize()) {
1403 gfx::Size scale(double(aSize.width) / finalSize.width,
1404 double(aSize.height) / finalSize.height);
1405 aContext->Multiply(gfxMatrix::Scaling(scale.width, scale.height));
1406 region.Scale(1.0 / scale.width, 1.0 / scale.height);
1408 couldRedecodeForBetterFrame = CanDownscaleDuringDecode(aSize, aFlags);
1411 if (!aSurface->Draw(aContext, region, aSamplingFilter, aFlags, aOpacity)) {
1412 RecoverFromInvalidFrames(aSize, aFlags);
1413 return ImgDrawResult::TEMPORARY_ERROR;
1415 if (!frameIsFinished) {
1416 return ImgDrawResult::INCOMPLETE;
1418 if (couldRedecodeForBetterFrame) {
1419 return ImgDrawResult::WRONG_SIZE;
1421 return ImgDrawResult::SUCCESS;
1424 //******************************************************************************
1425 NS_IMETHODIMP_(ImgDrawResult)
1426 RasterImage::Draw(gfxContext* aContext, const IntSize& aSize,
1427 const ImageRegion& aRegion, uint32_t aWhichFrame,
1428 SamplingFilter aSamplingFilter,
1429 const Maybe<SVGImageContext>& /*aSVGContext - ignored*/,
1430 uint32_t aFlags, float aOpacity) {
1431 if (aWhichFrame > FRAME_MAX_VALUE) {
1432 return ImgDrawResult::BAD_ARGS;
1435 if (mError) {
1436 return ImgDrawResult::BAD_IMAGE;
1439 // Illegal -- you can't draw with non-default decode flags.
1440 // (Disabling colorspace conversion might make sense to allow, but
1441 // we don't currently.)
1442 if (ToSurfaceFlags(aFlags) != DefaultSurfaceFlags()) {
1443 return ImgDrawResult::BAD_ARGS;
1446 if (!aContext) {
1447 return ImgDrawResult::BAD_ARGS;
1450 if (mAnimationConsumers == 0) {
1451 SendOnUnlockedDraw(aFlags);
1454 // If we're not using SamplingFilter::GOOD, we shouldn't high-quality scale or
1455 // downscale during decode.
1456 uint32_t flags = aSamplingFilter == SamplingFilter::GOOD
1457 ? aFlags
1458 : aFlags & ~FLAG_HIGH_QUALITY_SCALING;
1460 auto size = ToUnoriented(OrientedIntSize::FromUnknownSize(aSize));
1461 LookupResult result = LookupFrame(size, flags, ToPlaybackType(aWhichFrame),
1462 /* aMarkUsed = */ true);
1463 if (!result) {
1464 // Getting the frame (above) touches the image and kicks off decoding.
1465 if (mDrawStartTime.IsNull()) {
1466 mDrawStartTime = TimeStamp::Now();
1468 return ImgDrawResult::NOT_READY;
1471 bool shouldRecordTelemetry =
1472 !mDrawStartTime.IsNull() && result.Surface()->IsFinished();
1474 ImgDrawResult drawResult;
1476 gfxContextMatrixAutoSaveRestore asr;
1477 ImageRegion region(aRegion);
1479 if (!mOrientation.IsIdentity()) {
1480 // Apply a transform so that the unoriented image is drawn in the
1481 // orientation expected by the caller.
1482 gfxMatrix matrix = OrientationMatrix(size);
1483 asr.SetContext(aContext);
1484 aContext->Multiply(matrix);
1486 // Convert the region to unoriented coordinates.
1487 gfxMatrix inverseMatrix = OrientationMatrix(size, /* aInvert = */ true);
1488 region.TransformBoundsBy(inverseMatrix);
1491 drawResult = DrawInternal(std::move(result.Surface()), aContext, size,
1492 region, aSamplingFilter, flags, aOpacity);
1495 if (shouldRecordTelemetry) {
1496 TimeDuration drawLatency = TimeStamp::Now() - mDrawStartTime;
1497 Telemetry::Accumulate(Telemetry::IMAGE_DECODE_ON_DRAW_LATENCY,
1498 int32_t(drawLatency.ToMicroseconds()));
1499 mDrawStartTime = TimeStamp();
1502 return drawResult;
1505 //******************************************************************************
1507 NS_IMETHODIMP
1508 RasterImage::LockImage() {
1509 MOZ_ASSERT(NS_IsMainThread(),
1510 "Main thread to encourage serialization with UnlockImage");
1511 if (mError) {
1512 return NS_ERROR_FAILURE;
1515 // Increment the lock count
1516 mLockCount++;
1518 // Lock this image's surfaces in the SurfaceCache.
1519 if (mLockCount == 1) {
1520 SurfaceCache::LockImage(ImageKey(this));
1523 return NS_OK;
1526 //******************************************************************************
1528 NS_IMETHODIMP
1529 RasterImage::UnlockImage() {
1530 MOZ_ASSERT(NS_IsMainThread(),
1531 "Main thread to encourage serialization with LockImage");
1532 if (mError) {
1533 return NS_ERROR_FAILURE;
1536 // It's an error to call this function if the lock count is 0
1537 MOZ_ASSERT(mLockCount > 0, "Calling UnlockImage with mLockCount == 0!");
1538 if (mLockCount == 0) {
1539 return NS_ERROR_ABORT;
1542 // Decrement our lock count
1543 mLockCount--;
1545 // Unlock this image's surfaces in the SurfaceCache.
1546 if (mLockCount == 0) {
1547 SurfaceCache::UnlockImage(ImageKey(this));
1550 return NS_OK;
1553 //******************************************************************************
1555 NS_IMETHODIMP
1556 RasterImage::RequestDiscard() {
1557 if (LoadDiscardable() && // Enabled at creation time...
1558 mLockCount == 0 && // ...not temporarily disabled...
1559 CanDiscard()) {
1560 Discard();
1563 return NS_OK;
1566 // Idempotent error flagging routine. If a decoder is open, shuts it down.
1567 void RasterImage::DoError() {
1568 // If we've flagged an error before, we have nothing to do
1569 if (mError) {
1570 return;
1573 // We can't safely handle errors off-main-thread, so dispatch a worker to
1574 // do it.
1575 if (!NS_IsMainThread()) {
1576 HandleErrorWorker::DispatchIfNeeded(this);
1577 return;
1580 // Put the container in an error state.
1581 mError = true;
1583 // Stop animation and release our FrameAnimator.
1584 if (mAnimating) {
1585 StopAnimation();
1587 mAnimationState = Nothing();
1588 mFrameAnimator = nullptr;
1590 // Release all locks.
1591 mLockCount = 0;
1592 SurfaceCache::UnlockImage(ImageKey(this));
1594 // Release all frames from the surface cache.
1595 SurfaceCache::RemoveImage(ImageKey(this));
1597 // Invalidate to get rid of any partially-drawn image content.
1598 auto dirtyRect = UnorientedIntRect({0, 0}, ToUnoriented(mSize));
1599 NotifyProgress(NoProgress, dirtyRect);
1601 MOZ_LOG(gImgLog, LogLevel::Error,
1602 ("RasterImage: [this=%p] Error detected for image\n", this));
1605 /* static */
1606 void RasterImage::HandleErrorWorker::DispatchIfNeeded(RasterImage* aImage) {
1607 RefPtr<HandleErrorWorker> worker = new HandleErrorWorker(aImage);
1608 NS_DispatchToMainThread(worker);
1611 RasterImage::HandleErrorWorker::HandleErrorWorker(RasterImage* aImage)
1612 : Runnable("image::RasterImage::HandleErrorWorker"), mImage(aImage) {
1613 MOZ_ASSERT(mImage, "Should have image");
1616 NS_IMETHODIMP
1617 RasterImage::HandleErrorWorker::Run() {
1618 mImage->DoError();
1620 return NS_OK;
1623 bool RasterImage::ShouldAnimate() {
1624 return ImageResource::ShouldAnimate() && mAnimationState &&
1625 mAnimationState->KnownFrameCount() >= 1 && !LoadAnimationFinished();
1628 #ifdef DEBUG
1629 NS_IMETHODIMP
1630 RasterImage::GetFramesNotified(uint32_t* aFramesNotified) {
1631 NS_ENSURE_ARG_POINTER(aFramesNotified);
1633 *aFramesNotified = mFramesNotified;
1635 return NS_OK;
1637 #endif
1639 void RasterImage::NotifyProgress(
1640 Progress aProgress,
1641 const UnorientedIntRect& aInvalidRect /* = UnorientedIntRect() */,
1642 const Maybe<uint32_t>& aFrameCount /* = Nothing() */,
1643 DecoderFlags aDecoderFlags /* = DefaultDecoderFlags() */,
1644 SurfaceFlags aSurfaceFlags /* = DefaultSurfaceFlags() */) {
1645 MOZ_ASSERT(NS_IsMainThread());
1647 // Ensure that we stay alive long enough to finish notifying.
1648 RefPtr<RasterImage> image = this;
1650 UnorientedIntRect invalidRect = aInvalidRect;
1652 if (!(aDecoderFlags & DecoderFlags::FIRST_FRAME_ONLY)) {
1653 // We may have decoded new animation frames; update our animation state.
1654 MOZ_ASSERT_IF(aFrameCount && *aFrameCount > 1, mAnimationState || mError);
1655 if (mAnimationState && aFrameCount) {
1656 mAnimationState->UpdateKnownFrameCount(*aFrameCount);
1659 // If we should start animating right now, do so.
1660 if (mAnimationState && aFrameCount == Some(1u) && LoadPendingAnimation() &&
1661 ShouldAnimate()) {
1662 StartAnimation();
1665 if (mAnimationState) {
1666 auto size = ToUnoriented(mSize);
1667 IntRect rect = mAnimationState->UpdateState(this, size.ToUnknownSize());
1669 invalidRect.UnionRect(invalidRect,
1670 UnorientedIntRect::FromUnknownRect(rect));
1674 const bool wasDefaultFlags = aSurfaceFlags == DefaultSurfaceFlags();
1676 auto orientedInvalidRect = ToOriented(invalidRect);
1678 if (!orientedInvalidRect.IsEmpty() && wasDefaultFlags) {
1679 // Update our image container since we're invalidating.
1680 UpdateImageContainer(Some(orientedInvalidRect.ToUnknownRect()));
1683 // Tell the observers what happened.
1684 image->mProgressTracker->SyncNotifyProgress(
1685 aProgress, orientedInvalidRect.ToUnknownRect());
1688 void RasterImage::NotifyDecodeComplete(
1689 const DecoderFinalStatus& aStatus, const ImageMetadata& aMetadata,
1690 const DecoderTelemetry& aTelemetry, Progress aProgress,
1691 const UnorientedIntRect& aInvalidRect, const Maybe<uint32_t>& aFrameCount,
1692 DecoderFlags aDecoderFlags, SurfaceFlags aSurfaceFlags) {
1693 MOZ_ASSERT(NS_IsMainThread());
1695 // If the decoder detected an error, log it to the error console.
1696 if (aStatus.mShouldReportError) {
1697 ReportDecoderError();
1700 // Record all the metadata the decoder gathered about this image.
1701 bool metadataOK = SetMetadata(aMetadata, aStatus.mWasMetadataDecode);
1702 if (!metadataOK) {
1703 // This indicates a serious error that requires us to discard all existing
1704 // surfaces and redecode to recover. We'll drop the results from this
1705 // decoder on the floor, since they aren't valid.
1706 RecoverFromInvalidFrames(ToUnoriented(mSize),
1707 FromSurfaceFlags(aSurfaceFlags));
1708 return;
1711 MOZ_ASSERT(mError || LoadHasSize() || !aMetadata.HasSize(),
1712 "SetMetadata should've gotten a size");
1714 if (!aStatus.mWasMetadataDecode && aStatus.mFinished) {
1715 // Flag that we've been decoded before.
1716 StoreHasBeenDecoded(true);
1719 // Send out any final notifications.
1720 NotifyProgress(aProgress, aInvalidRect, aFrameCount, aDecoderFlags,
1721 aSurfaceFlags);
1723 if (!(aDecoderFlags & DecoderFlags::FIRST_FRAME_ONLY)) {
1724 // We may have decoded new animation frames; update our animation state.
1725 MOZ_ASSERT_IF(aFrameCount && *aFrameCount > 1, mAnimationState || mError);
1726 if (mAnimationState && aFrameCount) {
1727 mAnimationState->UpdateKnownFrameCount(*aFrameCount);
1730 // If we should start animating right now, do so.
1731 if (mAnimationState && aFrameCount == Some(1u) && LoadPendingAnimation() &&
1732 ShouldAnimate()) {
1733 StartAnimation();
1736 if (mAnimationState && LoadHasBeenDecoded()) {
1737 // We've finished a full decode of all animation frames and our
1738 // AnimationState has been notified about them all, so let it know not to
1739 // expect anymore.
1740 mAnimationState->NotifyDecodeComplete();
1742 auto size = ToUnoriented(mSize);
1743 IntRect rect = mAnimationState->UpdateState(this, size.ToUnknownSize());
1745 if (!rect.IsEmpty()) {
1746 auto dirtyRect = UnorientedIntRect::FromUnknownRect(rect);
1747 NotifyProgress(NoProgress, dirtyRect);
1752 // Do some telemetry if this isn't a metadata decode.
1753 if (!aStatus.mWasMetadataDecode) {
1754 if (aTelemetry.mChunkCount) {
1755 Telemetry::Accumulate(Telemetry::IMAGE_DECODE_CHUNKS,
1756 aTelemetry.mChunkCount);
1759 if (aStatus.mFinished) {
1760 Telemetry::Accumulate(Telemetry::IMAGE_DECODE_TIME,
1761 int32_t(aTelemetry.mDecodeTime.ToMicroseconds()));
1763 if (aTelemetry.mSpeedHistogram && aTelemetry.mBytesDecoded) {
1764 Telemetry::Accumulate(*aTelemetry.mSpeedHistogram, aTelemetry.Speed());
1769 // Only act on errors if we have no usable frames from the decoder.
1770 if (aStatus.mHadError &&
1771 (!mAnimationState || mAnimationState->KnownFrameCount() == 0)) {
1772 DoError();
1773 } else if (aStatus.mWasMetadataDecode && !LoadHasSize()) {
1774 DoError();
1777 // XXX(aosmond): Can we get this far without mFinished == true?
1778 if (aStatus.mFinished && aStatus.mWasMetadataDecode) {
1779 // If we were waiting to fire the load event, go ahead and fire it now.
1780 if (mLoadProgress) {
1781 NotifyForLoadEvent(*mLoadProgress);
1782 mLoadProgress = Nothing();
1785 // If we were a metadata decode and a full decode was requested, do it.
1786 if (LoadWantFullDecode()) {
1787 StoreWantFullDecode(false);
1788 RequestDecodeForSize(mSize.ToUnknownSize(),
1789 DECODE_FLAGS_DEFAULT | FLAG_HIGH_QUALITY_SCALING,
1790 FRAME_CURRENT);
1795 void RasterImage::ReportDecoderError() {
1796 nsCOMPtr<nsIConsoleService> consoleService =
1797 do_GetService(NS_CONSOLESERVICE_CONTRACTID);
1798 nsCOMPtr<nsIScriptError> errorObject =
1799 do_CreateInstance(NS_SCRIPTERROR_CONTRACTID);
1801 if (consoleService && errorObject) {
1802 nsAutoString msg(u"Image corrupt or truncated."_ns);
1803 nsAutoString src;
1804 if (GetURI()) {
1805 nsAutoCString uri;
1806 if (!GetSpecTruncatedTo1k(uri)) {
1807 msg += u" URI in this note truncated due to length."_ns;
1809 CopyUTF8toUTF16(uri, src);
1811 if (NS_SUCCEEDED(errorObject->InitWithWindowID(msg, src, u""_ns, 0, 0,
1812 nsIScriptError::errorFlag,
1813 "Image", InnerWindowID()))) {
1814 consoleService->LogMessage(errorObject);
1819 already_AddRefed<imgIContainer> RasterImage::Unwrap() {
1820 nsCOMPtr<imgIContainer> self(this);
1821 return self.forget();
1824 void RasterImage::PropagateUseCounters(dom::Document*) {
1825 // No use counters.
1828 IntSize RasterImage::OptimalImageSizeForDest(const gfxSize& aDest,
1829 uint32_t aWhichFrame,
1830 SamplingFilter aSamplingFilter,
1831 uint32_t aFlags) {
1832 MOZ_ASSERT(aDest.width >= 0 || ceil(aDest.width) <= INT32_MAX ||
1833 aDest.height >= 0 || ceil(aDest.height) <= INT32_MAX,
1834 "Unexpected destination size");
1836 if (mSize.IsEmpty() || aDest.IsEmpty()) {
1837 return IntSize(0, 0);
1840 auto dest = OrientedIntSize::FromUnknownSize(
1841 IntSize::Ceil(aDest.width, aDest.height));
1843 if (aSamplingFilter == SamplingFilter::GOOD &&
1844 CanDownscaleDuringDecode(ToUnoriented(dest), aFlags)) {
1845 return dest.ToUnknownSize();
1848 // We can't scale to this size. Use our intrinsic size for now.
1849 return mSize.ToUnknownSize();
1852 gfxMatrix RasterImage::OrientationMatrix(const UnorientedIntSize& aSize,
1853 bool aInvert) const {
1854 return OrientedImage::OrientationMatrix(mOrientation, aSize.ToUnknownSize(),
1855 aInvert);
1859 * Rotate aRect by the given angle within the space specified by aSize.
1861 * For example, with aRect = [20, 10, 5, 5] and aSize = [100, 100], rotating
1862 * with Angle::D90 will result in aRect = [85, 20, 5, 5].
1864 static void Rotate(IntRect& aRect, const IntSize& aSize, Angle aAngle) {
1865 switch (aAngle) {
1866 case Angle::D0:
1867 break;
1868 case Angle::D90:
1869 aRect = {aSize.height - aRect.YMost(), aRect.x, aRect.height,
1870 aRect.width};
1871 break;
1872 case Angle::D180:
1873 aRect.MoveTo(aSize.width - aRect.XMost(), aSize.height - aRect.YMost());
1874 break;
1875 case Angle::D270:
1876 aRect = {aRect.y, aSize.width - aRect.XMost(), aRect.height, aRect.width};
1877 break;
1882 * Flip aRect along the central axis within aSize.
1884 * For example, with aRect = [20, 10, 5, 5] and aSize = [100, 100], flipping
1885 * with Flip::Horizontal will result in aRect = [75, 10, 5, 5].
1887 static void Flip(IntRect& aRect, const IntSize& aSize, Flip aFlip) {
1888 switch (aFlip) {
1889 case Flip::Unflipped:
1890 break;
1891 case Flip::Horizontal:
1892 aRect.x = aSize.width - aRect.XMost();
1893 break;
1897 OrientedIntRect RasterImage::ToOriented(UnorientedIntRect aRect) const {
1898 IntRect rect = aRect.ToUnknownRect();
1899 auto size = ToUnoriented(mSize);
1901 MOZ_ASSERT(!mOrientation.flipFirst,
1902 "flipFirst should only be used by OrientedImage");
1904 // mOrientation specifies the transformation from a correctly oriented image
1905 // to the pixels stored in the file, so we need to rotate by the negation of
1906 // the given angle.
1907 Angle angle = Orientation::InvertAngle(mOrientation.rotation);
1908 Rotate(rect, size.ToUnknownSize(), angle);
1910 // Use mSize instead of size, since after the Rotate call, the size of the
1911 // space that rect is in has had its width and height swapped.
1912 Flip(rect, mSize.ToUnknownSize(), mOrientation.flip);
1914 return OrientedIntRect::FromUnknownRect(rect);
1917 UnorientedIntRect RasterImage::ToUnoriented(OrientedIntRect aRect) const {
1918 IntRect rect = aRect.ToUnknownRect();
1920 Flip(rect, mSize.ToUnknownSize(), mOrientation.flip);
1921 Rotate(rect, mSize.ToUnknownSize(), mOrientation.rotation);
1923 MOZ_ASSERT(!mOrientation.flipFirst,
1924 "flipFirst should only be used by OrientedImage");
1926 return UnorientedIntRect::FromUnknownRect(rect);
1929 } // namespace image
1930 } // namespace mozilla