Bumping manifests a=b2g-bump
[gecko.git] / image / src / Image.cpp
blobdd9430521a828adf581b40f21cc5a282de9a46b1
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"
8 #include "Image.h"
9 #include "nsRefreshDriver.h"
10 #include "mozilla/TimeStamp.h"
12 namespace mozilla {
13 namespace image {
15 // Constructor
16 ImageResource::ImageResource(ImageURL* aURI) :
17 mURI(aURI),
18 mInnerWindowId(0),
19 mAnimationConsumers(0),
20 mAnimationMode(kNormalAnimMode),
21 mInitialized(false),
22 mAnimating(false),
23 mError(false)
27 uint32_t
28 ImageResource::SizeOfData()
30 if (mError)
31 return 0;
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
43 Image::eDecoderType
44 Image::GetDecoderType(const char *aMimeType)
46 // By default we don't know
47 eDecoderType rv = eDecoderType_unknown;
49 // PNG
50 if (!strcmp(aMimeType, IMAGE_PNG))
51 rv = eDecoderType_png;
52 else if (!strcmp(aMimeType, IMAGE_X_PNG))
53 rv = eDecoderType_png;
55 // GIF
56 else if (!strcmp(aMimeType, IMAGE_GIF))
57 rv = eDecoderType_gif;
60 // JPEG
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;
68 // BMP
69 else if (!strcmp(aMimeType, IMAGE_BMP))
70 rv = eDecoderType_bmp;
71 else if (!strcmp(aMimeType, IMAGE_BMP_MS))
72 rv = eDecoderType_bmp;
75 // ICO
76 else if (!strcmp(aMimeType, IMAGE_ICO))
77 rv = eDecoderType_ico;
78 else if (!strcmp(aMimeType, IMAGE_ICO_MS))
79 rv = eDecoderType_ico;
81 // Icon
82 else if (!strcmp(aMimeType, IMAGE_ICON_MS))
83 rv = eDecoderType_icon;
85 return rv;
88 void
89 ImageResource::IncrementAnimationConsumers()
91 MOZ_ASSERT(NS_IsMainThread(), "Main thread only to encourage serialization "
92 "with DecrementAnimationConsumers");
93 mAnimationConsumers++;
96 void
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--;
105 nsresult
106 ImageResource::GetAnimationModeInternal(uint16_t* aAnimationMode)
108 if (mError)
109 return NS_ERROR_FAILURE;
111 NS_ENSURE_ARG_POINTER(aAnimationMode);
113 *aAnimationMode = mAnimationMode;
114 return NS_OK;
117 nsresult
118 ImageResource::SetAnimationModeInternal(uint16_t aAnimationMode)
120 if (mError)
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;
130 return NS_OK;
133 bool
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) {
144 return true;
147 // else, we can proceed with a refresh.
148 // But first, update our last refresh time:
149 mLastRefreshTime = aTime;
150 return false;
153 void
154 ImageResource::EvaluateAnimation()
156 if (!mAnimating && ShouldAnimate()) {
157 nsresult rv = StartAnimation();
158 mAnimating = NS_SUCCEEDED(rv);
159 } else if (mAnimating && !ShouldAnimate()) {
160 StopAnimation();
164 } // namespace image
165 } // namespace mozilla