1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * the Mozilla Foundation.
19 : * Portions created by the Initial Developer are Copyright (C) 2010.
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "Image.h"
39 :
40 : namespace mozilla {
41 : namespace image {
42 :
43 : // Constructor
44 24 : Image::Image(imgStatusTracker* aStatusTracker) :
45 : mInnerWindowId(0),
46 : mAnimationConsumers(0),
47 : mAnimationMode(kNormalAnimMode),
48 : mInitialized(false),
49 : mAnimating(false),
50 24 : mError(false)
51 : {
52 24 : if (aStatusTracker) {
53 8 : mStatusTracker = aStatusTracker;
54 8 : mStatusTracker->SetImage(this);
55 : } else {
56 16 : mStatusTracker = new imgStatusTracker(this);
57 : }
58 24 : }
59 :
60 : PRUint32
61 6 : Image::SizeOfData()
62 : {
63 6 : if (mError)
64 0 : return 0;
65 :
66 : // This is not used by memory reporters, but for sizing the cache, which is
67 : // why it uses |moz_malloc_size_of| rather than an
68 : // |NS_MEMORY_REPORTER_MALLOC_SIZEOF_FUN|.
69 6 : return PRUint32(HeapSizeOfSourceWithComputedFallback(moz_malloc_size_of) +
70 6 : HeapSizeOfDecodedWithComputedFallback(moz_malloc_size_of) +
71 6 : NonHeapSizeOfDecoded() +
72 12 : OutOfProcessSizeOfDecoded());
73 : }
74 :
75 : // Translates a mimetype into a concrete decoder
76 : Image::eDecoderType
77 24 : Image::GetDecoderType(const char *aMimeType)
78 : {
79 : // By default we don't know
80 24 : eDecoderType rv = eDecoderType_unknown;
81 :
82 : // PNG
83 24 : if (!strcmp(aMimeType, "image/png"))
84 6 : rv = eDecoderType_png;
85 18 : else if (!strcmp(aMimeType, "image/x-png"))
86 0 : rv = eDecoderType_png;
87 :
88 : // GIF
89 18 : else if (!strcmp(aMimeType, "image/gif"))
90 4 : rv = eDecoderType_gif;
91 :
92 :
93 : // JPEG
94 14 : else if (!strcmp(aMimeType, "image/jpeg"))
95 5 : rv = eDecoderType_jpeg;
96 9 : else if (!strcmp(aMimeType, "image/pjpeg"))
97 0 : rv = eDecoderType_jpeg;
98 9 : else if (!strcmp(aMimeType, "image/jpg"))
99 0 : rv = eDecoderType_jpeg;
100 :
101 : // BMP
102 9 : else if (!strcmp(aMimeType, "image/bmp"))
103 0 : rv = eDecoderType_bmp;
104 9 : else if (!strcmp(aMimeType, "image/x-ms-bmp"))
105 0 : rv = eDecoderType_bmp;
106 :
107 :
108 : // ICO
109 9 : else if (!strcmp(aMimeType, "image/x-icon"))
110 5 : rv = eDecoderType_ico;
111 4 : else if (!strcmp(aMimeType, "image/vnd.microsoft.icon"))
112 0 : rv = eDecoderType_ico;
113 :
114 : // Icon
115 4 : else if (!strcmp(aMimeType, "image/icon"))
116 0 : rv = eDecoderType_icon;
117 :
118 24 : return rv;
119 : }
120 :
121 : void
122 0 : Image::IncrementAnimationConsumers()
123 : {
124 0 : mAnimationConsumers++;
125 0 : EvaluateAnimation();
126 0 : }
127 :
128 : void
129 0 : Image::DecrementAnimationConsumers()
130 : {
131 0 : NS_ABORT_IF_FALSE(mAnimationConsumers >= 1, "Invalid no. of animation consumers!");
132 0 : mAnimationConsumers--;
133 0 : EvaluateAnimation();
134 0 : }
135 :
136 : //******************************************************************************
137 : /* attribute unsigned short animationMode; */
138 : NS_IMETHODIMP
139 0 : Image::GetAnimationMode(PRUint16* aAnimationMode)
140 : {
141 0 : if (mError)
142 0 : return NS_ERROR_FAILURE;
143 :
144 0 : NS_ENSURE_ARG_POINTER(aAnimationMode);
145 :
146 0 : *aAnimationMode = mAnimationMode;
147 0 : return NS_OK;
148 : }
149 :
150 : //******************************************************************************
151 : /* attribute unsigned short animationMode; */
152 : NS_IMETHODIMP
153 0 : Image::SetAnimationMode(PRUint16 aAnimationMode)
154 : {
155 0 : if (mError)
156 0 : return NS_ERROR_FAILURE;
157 :
158 0 : NS_ASSERTION(aAnimationMode == kNormalAnimMode ||
159 : aAnimationMode == kDontAnimMode ||
160 : aAnimationMode == kLoopOnceAnimMode,
161 : "Wrong Animation Mode is being set!");
162 :
163 0 : mAnimationMode = aAnimationMode;
164 :
165 0 : EvaluateAnimation();
166 :
167 0 : return NS_OK;
168 : }
169 :
170 : void
171 2 : Image::EvaluateAnimation()
172 : {
173 2 : if (!mAnimating && ShouldAnimate()) {
174 0 : nsresult rv = StartAnimation();
175 0 : mAnimating = NS_SUCCEEDED(rv);
176 2 : } else if (mAnimating && !ShouldAnimate()) {
177 0 : StopAnimation();
178 0 : mAnimating = false;
179 : }
180 2 : }
181 :
182 : } // namespace image
183 : } // namespace mozilla
|