image/src/Image.cpp

branch
TOR_BUG_9701
changeset 10
ac0c01689b40
equal deleted inserted replaced
-1:000000000000 0:a3bf221f7371
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/. */
5
6 #include "nsMimeTypes.h"
7
8 #include "Image.h"
9
10 namespace mozilla {
11 namespace image {
12
13 // Constructor
14 ImageResource::ImageResource(ImageURL* aURI) :
15 mURI(aURI),
16 mInnerWindowId(0),
17 mAnimationConsumers(0),
18 mAnimationMode(kNormalAnimMode),
19 mInitialized(false),
20 mAnimating(false),
21 mError(false)
22 {
23 }
24
25 uint32_t
26 ImageResource::SizeOfData()
27 {
28 if (mError)
29 return 0;
30
31 // This is not used by memory reporters, but for sizing the cache, which is
32 // why it uses |moz_malloc_size_of| rather than a
33 // |MOZ_DEFINE_MALLOC_SIZE_OF|.
34 return uint32_t(HeapSizeOfSourceWithComputedFallback(moz_malloc_size_of) +
35 HeapSizeOfDecodedWithComputedFallback(moz_malloc_size_of) +
36 NonHeapSizeOfDecoded() +
37 OutOfProcessSizeOfDecoded());
38 }
39
40 // Translates a mimetype into a concrete decoder
41 Image::eDecoderType
42 Image::GetDecoderType(const char *aMimeType)
43 {
44 // By default we don't know
45 eDecoderType rv = eDecoderType_unknown;
46
47 // PNG
48 if (!strcmp(aMimeType, IMAGE_PNG))
49 rv = eDecoderType_png;
50 else if (!strcmp(aMimeType, IMAGE_X_PNG))
51 rv = eDecoderType_png;
52
53 // GIF
54 else if (!strcmp(aMimeType, IMAGE_GIF))
55 rv = eDecoderType_gif;
56
57
58 // JPEG
59 else if (!strcmp(aMimeType, IMAGE_JPEG))
60 rv = eDecoderType_jpeg;
61 else if (!strcmp(aMimeType, IMAGE_PJPEG))
62 rv = eDecoderType_jpeg;
63 else if (!strcmp(aMimeType, IMAGE_JPG))
64 rv = eDecoderType_jpeg;
65
66 // BMP
67 else if (!strcmp(aMimeType, IMAGE_BMP))
68 rv = eDecoderType_bmp;
69 else if (!strcmp(aMimeType, IMAGE_BMP_MS))
70 rv = eDecoderType_bmp;
71
72
73 // ICO
74 else if (!strcmp(aMimeType, IMAGE_ICO))
75 rv = eDecoderType_ico;
76 else if (!strcmp(aMimeType, IMAGE_ICO_MS))
77 rv = eDecoderType_ico;
78
79 // Icon
80 else if (!strcmp(aMimeType, IMAGE_ICON_MS))
81 rv = eDecoderType_icon;
82
83 return rv;
84 }
85
86 void
87 ImageResource::IncrementAnimationConsumers()
88 {
89 MOZ_ASSERT(NS_IsMainThread(), "Main thread only to encourage serialization "
90 "with DecrementAnimationConsumers");
91 mAnimationConsumers++;
92 }
93
94 void
95 ImageResource::DecrementAnimationConsumers()
96 {
97 MOZ_ASSERT(NS_IsMainThread(), "Main thread only to encourage serialization "
98 "with IncrementAnimationConsumers");
99 NS_ABORT_IF_FALSE(mAnimationConsumers >= 1, "Invalid no. of animation consumers!");
100 mAnimationConsumers--;
101 }
102
103 nsresult
104 ImageResource::GetAnimationModeInternal(uint16_t* aAnimationMode)
105 {
106 if (mError)
107 return NS_ERROR_FAILURE;
108
109 NS_ENSURE_ARG_POINTER(aAnimationMode);
110
111 *aAnimationMode = mAnimationMode;
112 return NS_OK;
113 }
114
115 nsresult
116 ImageResource::SetAnimationModeInternal(uint16_t aAnimationMode)
117 {
118 if (mError)
119 return NS_ERROR_FAILURE;
120
121 NS_ASSERTION(aAnimationMode == kNormalAnimMode ||
122 aAnimationMode == kDontAnimMode ||
123 aAnimationMode == kLoopOnceAnimMode,
124 "Wrong Animation Mode is being set!");
125
126 mAnimationMode = aAnimationMode;
127
128 return NS_OK;
129 }
130
131 void
132 ImageResource::EvaluateAnimation()
133 {
134 if (!mAnimating && ShouldAnimate()) {
135 nsresult rv = StartAnimation();
136 mAnimating = NS_SUCCEEDED(rv);
137 } else if (mAnimating && !ShouldAnimate()) {
138 StopAnimation();
139 }
140 }
141
142 } // namespace image
143 } // namespace mozilla

mercurial