|
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 #ifndef MOZILLA_IMAGELIB_IMAGE_H_ |
|
7 #define MOZILLA_IMAGELIB_IMAGE_H_ |
|
8 |
|
9 #include "mozilla/MemoryReporting.h" |
|
10 #include "imgIContainer.h" |
|
11 #include "imgStatusTracker.h" |
|
12 #include "ImageURL.h" |
|
13 |
|
14 class nsIRequest; |
|
15 class nsIInputStream; |
|
16 |
|
17 namespace mozilla { |
|
18 namespace image { |
|
19 |
|
20 class Image : public imgIContainer |
|
21 { |
|
22 public: |
|
23 // Mimetype translation |
|
24 enum eDecoderType { |
|
25 eDecoderType_png = 0, |
|
26 eDecoderType_gif = 1, |
|
27 eDecoderType_jpeg = 2, |
|
28 eDecoderType_bmp = 3, |
|
29 eDecoderType_ico = 4, |
|
30 eDecoderType_icon = 5, |
|
31 eDecoderType_unknown = 6 |
|
32 }; |
|
33 static eDecoderType GetDecoderType(const char *aMimeType); |
|
34 |
|
35 /** |
|
36 * Flags for Image initialization. |
|
37 * |
|
38 * Meanings: |
|
39 * |
|
40 * INIT_FLAG_NONE: Lack of flags |
|
41 * |
|
42 * INIT_FLAG_DISCARDABLE: The container should be discardable |
|
43 * |
|
44 * INIT_FLAG_DECODE_ON_DRAW: The container should decode on draw rather than |
|
45 * decoding on load. |
|
46 * |
|
47 * INIT_FLAG_MULTIPART: The container will be used to display a stream of |
|
48 * images in a multipart channel. If this flag is set, INIT_FLAG_DISCARDABLE |
|
49 * and INIT_FLAG_DECODE_ON_DRAW must not be set. |
|
50 */ |
|
51 static const uint32_t INIT_FLAG_NONE = 0x0; |
|
52 static const uint32_t INIT_FLAG_DISCARDABLE = 0x1; |
|
53 static const uint32_t INIT_FLAG_DECODE_ON_DRAW = 0x2; |
|
54 static const uint32_t INIT_FLAG_MULTIPART = 0x4; |
|
55 |
|
56 /** |
|
57 * Creates a new image container. |
|
58 * |
|
59 * @param aMimeType The mimetype of the image. |
|
60 * @param aFlags Initialization flags of the INIT_FLAG_* variety. |
|
61 */ |
|
62 virtual nsresult Init(const char* aMimeType, |
|
63 uint32_t aFlags) = 0; |
|
64 |
|
65 virtual already_AddRefed<imgStatusTracker> GetStatusTracker() = 0; |
|
66 virtual void SetStatusTracker(imgStatusTracker* aStatusTracker) {} |
|
67 |
|
68 /** |
|
69 * The rectangle defining the location and size of the given frame. |
|
70 */ |
|
71 virtual nsIntRect FrameRect(uint32_t aWhichFrame) = 0; |
|
72 |
|
73 /** |
|
74 * The size, in bytes, occupied by the significant data portions of the image. |
|
75 * This includes both compressed source data and decoded frames. |
|
76 */ |
|
77 virtual uint32_t SizeOfData() = 0; |
|
78 |
|
79 /** |
|
80 * The components that make up SizeOfData(). |
|
81 */ |
|
82 virtual size_t HeapSizeOfSourceWithComputedFallback(mozilla::MallocSizeOf aMallocSizeOf) const = 0; |
|
83 virtual size_t HeapSizeOfDecodedWithComputedFallback(mozilla::MallocSizeOf aMallocSizeOf) const = 0; |
|
84 virtual size_t NonHeapSizeOfDecoded() const = 0; |
|
85 virtual size_t OutOfProcessSizeOfDecoded() const = 0; |
|
86 |
|
87 virtual void IncrementAnimationConsumers() = 0; |
|
88 virtual void DecrementAnimationConsumers() = 0; |
|
89 #ifdef DEBUG |
|
90 virtual uint32_t GetAnimationConsumers() = 0; |
|
91 #endif |
|
92 |
|
93 /** |
|
94 * Called from OnDataAvailable when the stream associated with the image has |
|
95 * received new image data. The arguments are the same as OnDataAvailable's, |
|
96 * but by separating this functionality into a different method we don't |
|
97 * interfere with subclasses which wish to implement nsIStreamListener. |
|
98 * |
|
99 * Images should not do anything that could send out notifications until they |
|
100 * have received their first OnImageDataAvailable notification; in |
|
101 * particular, this means that instantiating decoders should be deferred |
|
102 * until OnImageDataAvailable is called. |
|
103 */ |
|
104 virtual nsresult OnImageDataAvailable(nsIRequest* aRequest, |
|
105 nsISupports* aContext, |
|
106 nsIInputStream* aInStr, |
|
107 uint64_t aSourceOffset, |
|
108 uint32_t aCount) = 0; |
|
109 |
|
110 /** |
|
111 * Called from OnStopRequest when the image's underlying request completes. |
|
112 * |
|
113 * @param aRequest The completed request. |
|
114 * @param aContext Context from Necko's OnStopRequest. |
|
115 * @param aStatus A success or failure code. |
|
116 * @param aLastPart Whether this is the final part of the underlying request. |
|
117 */ |
|
118 virtual nsresult OnImageDataComplete(nsIRequest* aRequest, |
|
119 nsISupports* aContext, |
|
120 nsresult aStatus, |
|
121 bool aLastPart) = 0; |
|
122 |
|
123 /** |
|
124 * Called for multipart images to allow for any necessary reinitialization |
|
125 * when there's a new part to add. |
|
126 */ |
|
127 virtual nsresult OnNewSourceData() = 0; |
|
128 |
|
129 virtual void SetInnerWindowID(uint64_t aInnerWindowId) = 0; |
|
130 virtual uint64_t InnerWindowID() const = 0; |
|
131 |
|
132 virtual bool HasError() = 0; |
|
133 virtual void SetHasError() = 0; |
|
134 |
|
135 virtual ImageURL* GetURI() = 0; |
|
136 }; |
|
137 |
|
138 class ImageResource : public Image |
|
139 { |
|
140 public: |
|
141 already_AddRefed<imgStatusTracker> GetStatusTracker() MOZ_OVERRIDE { |
|
142 nsRefPtr<imgStatusTracker> statusTracker = mStatusTracker; |
|
143 MOZ_ASSERT(statusTracker); |
|
144 return statusTracker.forget(); |
|
145 } |
|
146 void SetStatusTracker(imgStatusTracker* aStatusTracker) MOZ_OVERRIDE MOZ_FINAL { |
|
147 MOZ_ASSERT(aStatusTracker); |
|
148 MOZ_ASSERT(!mStatusTracker); |
|
149 mStatusTracker = aStatusTracker; |
|
150 } |
|
151 virtual uint32_t SizeOfData() MOZ_OVERRIDE; |
|
152 |
|
153 virtual void IncrementAnimationConsumers() MOZ_OVERRIDE; |
|
154 virtual void DecrementAnimationConsumers() MOZ_OVERRIDE; |
|
155 #ifdef DEBUG |
|
156 virtual uint32_t GetAnimationConsumers() MOZ_OVERRIDE { return mAnimationConsumers; } |
|
157 #endif |
|
158 |
|
159 virtual void SetInnerWindowID(uint64_t aInnerWindowId) MOZ_OVERRIDE { |
|
160 mInnerWindowId = aInnerWindowId; |
|
161 } |
|
162 virtual uint64_t InnerWindowID() const MOZ_OVERRIDE { return mInnerWindowId; } |
|
163 |
|
164 virtual bool HasError() MOZ_OVERRIDE { return mError; } |
|
165 virtual void SetHasError() MOZ_OVERRIDE { mError = true; } |
|
166 |
|
167 /* |
|
168 * Returns a non-AddRefed pointer to the URI associated with this image. |
|
169 * Illegal to use off-main-thread. |
|
170 */ |
|
171 virtual ImageURL* GetURI() MOZ_OVERRIDE { return mURI.get(); } |
|
172 |
|
173 protected: |
|
174 ImageResource(ImageURL* aURI); |
|
175 |
|
176 // Shared functionality for implementors of imgIContainer. Every |
|
177 // implementation of attribute animationMode should forward here. |
|
178 nsresult GetAnimationModeInternal(uint16_t *aAnimationMode); |
|
179 nsresult SetAnimationModeInternal(uint16_t aAnimationMode); |
|
180 |
|
181 /** |
|
182 * Decides whether animation should or should not be happening, |
|
183 * and makes sure the right thing is being done. |
|
184 */ |
|
185 virtual void EvaluateAnimation(); |
|
186 |
|
187 /** |
|
188 * Extended by child classes, if they have additional |
|
189 * conditions for being able to animate. |
|
190 */ |
|
191 virtual bool ShouldAnimate() { |
|
192 return mAnimationConsumers > 0 && mAnimationMode != kDontAnimMode; |
|
193 } |
|
194 |
|
195 virtual nsresult StartAnimation() = 0; |
|
196 virtual nsresult StopAnimation() = 0; |
|
197 |
|
198 // Member data shared by all implementations of this abstract class |
|
199 nsRefPtr<imgStatusTracker> mStatusTracker; |
|
200 nsRefPtr<ImageURL> mURI; |
|
201 uint64_t mInnerWindowId; |
|
202 uint32_t mAnimationConsumers; |
|
203 uint16_t mAnimationMode; // Enum values in imgIContainer |
|
204 bool mInitialized:1; // Have we been initalized? |
|
205 bool mAnimating:1; // Are we currently animating? |
|
206 bool mError:1; // Error handling |
|
207 }; |
|
208 |
|
209 } // namespace image |
|
210 } // namespace mozilla |
|
211 |
|
212 #endif // MOZILLA_IMAGELIB_IMAGE_H_ |