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 #include "nsMimeTypes.h"
9 #include "nsRefreshDriver.h"
10 #include "mozilla/TimeStamp.h"
16 ImageResource::ImageResource(ImageURL
* aURI
) :
19 mAnimationConsumers(0),
20 mAnimationMode(kNormalAnimMode
),
28 ImageResource::SizeOfData()
33 // This is not used by memory reporters, but for sizing the cache, which is
34 // why it uses |moz_malloc_size_of| rather than a
35 // |MOZ_DEFINE_MALLOC_SIZE_OF|.
36 return uint32_t(HeapSizeOfSourceWithComputedFallback(moz_malloc_size_of
) +
37 HeapSizeOfDecodedWithComputedFallback(moz_malloc_size_of
) +
38 NonHeapSizeOfDecoded() +
39 OutOfProcessSizeOfDecoded());
42 // Translates a mimetype into a concrete decoder
44 Image::GetDecoderType(const char *aMimeType
)
46 // By default we don't know
47 eDecoderType rv
= eDecoderType_unknown
;
50 if (!strcmp(aMimeType
, IMAGE_PNG
))
51 rv
= eDecoderType_png
;
52 else if (!strcmp(aMimeType
, IMAGE_X_PNG
))
53 rv
= eDecoderType_png
;
56 else if (!strcmp(aMimeType
, IMAGE_GIF
))
57 rv
= eDecoderType_gif
;
61 else if (!strcmp(aMimeType
, IMAGE_JPEG
))
62 rv
= eDecoderType_jpeg
;
63 else if (!strcmp(aMimeType
, IMAGE_PJPEG
))
64 rv
= eDecoderType_jpeg
;
65 else if (!strcmp(aMimeType
, IMAGE_JPG
))
66 rv
= eDecoderType_jpeg
;
69 else if (!strcmp(aMimeType
, IMAGE_BMP
))
70 rv
= eDecoderType_bmp
;
71 else if (!strcmp(aMimeType
, IMAGE_BMP_MS
))
72 rv
= eDecoderType_bmp
;
76 else if (!strcmp(aMimeType
, IMAGE_ICO
))
77 rv
= eDecoderType_ico
;
78 else if (!strcmp(aMimeType
, IMAGE_ICO_MS
))
79 rv
= eDecoderType_ico
;
82 else if (!strcmp(aMimeType
, IMAGE_ICON_MS
))
83 rv
= eDecoderType_icon
;
89 ImageResource::IncrementAnimationConsumers()
91 MOZ_ASSERT(NS_IsMainThread(), "Main thread only to encourage serialization "
92 "with DecrementAnimationConsumers");
93 mAnimationConsumers
++;
97 ImageResource::DecrementAnimationConsumers()
99 MOZ_ASSERT(NS_IsMainThread(), "Main thread only to encourage serialization "
100 "with IncrementAnimationConsumers");
101 NS_ABORT_IF_FALSE(mAnimationConsumers
>= 1, "Invalid no. of animation consumers!");
102 mAnimationConsumers
--;
106 ImageResource::GetAnimationModeInternal(uint16_t* aAnimationMode
)
109 return NS_ERROR_FAILURE
;
111 NS_ENSURE_ARG_POINTER(aAnimationMode
);
113 *aAnimationMode
= mAnimationMode
;
118 ImageResource::SetAnimationModeInternal(uint16_t aAnimationMode
)
121 return NS_ERROR_FAILURE
;
123 NS_ASSERTION(aAnimationMode
== kNormalAnimMode
||
124 aAnimationMode
== kDontAnimMode
||
125 aAnimationMode
== kLoopOnceAnimMode
,
126 "Wrong Animation Mode is being set!");
128 mAnimationMode
= aAnimationMode
;
134 ImageResource::HadRecentRefresh(const TimeStamp
& aTime
)
136 // Our threshold for "recent" is 1/2 of the default refresh-driver interval.
137 // This ensures that we allow for frame rates at least as fast as the
138 // refresh driver's default rate.
139 static TimeDuration recentThreshold
=
140 TimeDuration::FromMilliseconds(nsRefreshDriver::DefaultInterval() / 2.0);
142 if (!mLastRefreshTime
.IsNull() &&
143 aTime
- mLastRefreshTime
< recentThreshold
) {
147 // else, we can proceed with a refresh.
148 // But first, update our last refresh time:
149 mLastRefreshTime
= aTime
;
154 ImageResource::EvaluateAnimation()
156 if (!mAnimating
&& ShouldAnimate()) {
157 nsresult rv
= StartAnimation();
158 mAnimating
= NS_SUCCEEDED(rv
);
159 } else if (mAnimating
&& !ShouldAnimate()) {
165 } // namespace mozilla