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 : * Bobby Holley <bobbyholley@gmail.com>
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #ifndef MOZILLA_IMAGELIB_DECODER_H_
40 : #define MOZILLA_IMAGELIB_DECODER_H_
41 :
42 : #include "RasterImage.h"
43 :
44 : #include "imgIDecoderObserver.h"
45 :
46 : namespace mozilla {
47 : namespace image {
48 :
49 : class Decoder
50 : {
51 : public:
52 :
53 : Decoder(RasterImage& aImage, imgIDecoderObserver* aObserver);
54 : virtual ~Decoder();
55 :
56 : /**
57 : * Initialize an image decoder. Decoders may not be re-initialized.
58 : *
59 : * @param aContainer The image container to decode to.
60 : * @param aObserver The observer for decode notification events.
61 : *
62 : * Notifications Sent: TODO
63 : */
64 : void Init();
65 :
66 :
67 : /**
68 : * Initializes a decoder whose aImage and aObserver is already being used by a
69 : * parent decoder. Decoders may not be re-initialized.
70 : *
71 : * @param aContainer The image container to decode to.
72 : * @param aObserver The observer for decode notification events.
73 : *
74 : * Notifications Sent: TODO
75 : */
76 : void InitSharedDecoder();
77 :
78 : /**
79 : * Writes data to the decoder.
80 : *
81 : * @param aBuffer buffer containing the data to be written
82 : * @param aCount the number of bytes to write
83 : *
84 : * Any errors are reported by setting the appropriate state on the decoder.
85 : *
86 : * Notifications Sent: TODO
87 : */
88 : void Write(const char* aBuffer, PRUint32 aCount);
89 :
90 : /**
91 : * Informs the decoder that all the data has been written.
92 : *
93 : * Notifications Sent: TODO
94 : */
95 : void Finish();
96 :
97 : /**
98 : * Informs the shared decoder that all the data has been written.
99 : * Should only be used if InitSharedDecoder was useed
100 : *
101 : * Notifications Sent: TODO
102 : */
103 : void FinishSharedDecoder();
104 :
105 : /**
106 : * Tells the decoder to flush any pending invalidations. This informs the image
107 : * frame of its decoded region, and sends the appropriate OnDataAvailable call
108 : * to consumers.
109 : *
110 : * This can be called any time when we're midway through decoding a frame,
111 : * and must be called after finishing a frame (before starting a new one).
112 : */
113 : void FlushInvalidations();
114 :
115 : // We're not COM-y, so we don't get refcounts by default
116 316 : NS_INLINE_DECL_REFCOUNTING(Decoder)
117 :
118 : /*
119 : * State.
120 : */
121 :
122 : // If we're doing a "size decode", we more or less pass through the image
123 : // data, stopping only to scoop out the image dimensions. A size decode
124 : // must be enabled by SetSizeDecode() _before_calling Init().
125 160 : bool IsSizeDecode() { return mSizeDecode; };
126 24 : void SetSizeDecode(bool aSizeDecode)
127 : {
128 24 : NS_ABORT_IF_FALSE(!mInitialized, "Can't set size decode after Init()!");
129 24 : mSizeDecode = aSizeDecode;
130 24 : }
131 :
132 : // The number of frames we have, including anything in-progress. Thus, this
133 : // is only 0 if we haven't begun any frames.
134 20 : PRUint32 GetFrameCount() { return mFrameCount; }
135 :
136 : // The number of complete frames we have (ie, not including anything in-progress).
137 0 : PRUint32 GetCompleteFrameCount() { return mInFrame ? mFrameCount - 1 : mFrameCount; }
138 :
139 : // Error tracking
140 125 : bool HasError() { return HasDataError() || HasDecoderError(); };
141 226 : bool HasDataError() { return mDataError; };
142 271 : bool HasDecoderError() { return NS_FAILED(mFailCode); };
143 99 : nsresult GetDecoderError() { return mFailCode; };
144 0 : void PostResizeError() { PostDataError(); }
145 4 : bool GetDecodeDone() const {
146 4 : return mDecodeDone;
147 : }
148 :
149 : // flags. Keep these in sync with imgIContainer.idl.
150 : // SetDecodeFlags must be called before Init(), otherwise
151 : // default flags are assumed.
152 : enum {
153 : DECODER_NO_PREMULTIPLY_ALPHA = 0x2,
154 : DECODER_NO_COLORSPACE_CONVERSION = 0x4
155 : };
156 20 : void SetDecodeFlags(PRUint32 aFlags) { mDecodeFlags = aFlags; }
157 0 : PRUint32 GetDecodeFlags() { return mDecodeFlags; }
158 :
159 : // Use HistogramCount as an invalid Histogram ID
160 0 : virtual Telemetry::ID SpeedHistogram() { return Telemetry::HistogramCount; }
161 :
162 : protected:
163 :
164 : /*
165 : * Internal hooks. Decoder implementations may override these and
166 : * only these methods.
167 : */
168 : virtual void InitInternal();
169 : virtual void WriteInternal(const char* aBuffer, PRUint32 aCount);
170 : virtual void FinishInternal();
171 :
172 : /*
173 : * Progress notifications.
174 : */
175 :
176 : // Called by decoders when they determine the size of the image. Informs
177 : // the image of its size and sends notifications.
178 : void PostSize(PRInt32 aWidth, PRInt32 aHeight);
179 :
180 : // Called by decoders when they begin/end a frame. Informs the image, sends
181 : // notifications, and does internal book-keeping.
182 : void PostFrameStart();
183 : void PostFrameStop();
184 :
185 : // Called by the decoders when they have a region to invalidate. We may not
186 : // actually pass these invalidations on right away.
187 : void PostInvalidation(nsIntRect& aRect);
188 :
189 : // Called by the decoders when they have successfully decoded the image. This
190 : // may occur as the result of the decoder getting to the appropriate point in
191 : // the stream, or by us calling FinishInternal().
192 : //
193 : // May not be called mid-frame.
194 : void PostDecodeDone();
195 :
196 : // Data errors are the fault of the source data, decoder errors are our fault
197 : void PostDataError();
198 : void PostDecoderError(nsresult aFailCode);
199 :
200 : /*
201 : * Member variables.
202 : *
203 : */
204 : RasterImage &mImage;
205 : nsCOMPtr<imgIDecoderObserver> mObserver;
206 :
207 : PRUint32 mDecodeFlags;
208 : bool mDecodeDone;
209 : bool mDataError;
210 :
211 : private:
212 : PRUint32 mFrameCount; // Number of frames, including anything in-progress
213 :
214 : nsIntRect mInvalidRect; // Tracks an invalidation region in the current frame.
215 :
216 : nsresult mFailCode;
217 :
218 : bool mInitialized;
219 : bool mSizeDecode;
220 : bool mInFrame;
221 : bool mIsAnimated;
222 : };
223 :
224 : } // namespace image
225 : } // namespace mozilla
226 :
227 : #endif // MOZILLA_IMAGELIB_DECODER_H_
|