OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved. | 2 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
11 * documentation and/or other materials provided with the distribution. | 11 * documentation and/or other materials provided with the distribution. |
12 * | 12 * |
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY | 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 */ | 24 */ |
25 | 25 |
26 #include "platform/image-decoders/gif/GIFImageDecoder.h" | 26 #include "platform/image-decoders/gif/GIFImageDecoder.h" |
27 | 27 |
28 #include <limits> | 28 #include <limits> |
29 #include "platform/image-decoders/gif/GIFImageReader.h" | 29 #include "platform/image-decoders/SegmentStream.h" |
30 #include "platform/wtf/NotFound.h" | 30 #include "platform/wtf/NotFound.h" |
31 #include "platform/wtf/PtrUtil.h" | 31 #include "platform/wtf/PtrUtil.h" |
32 #include "third_party/skia/include/core/SkImageInfo.h" | |
32 | 33 |
33 namespace blink { | 34 namespace blink { |
34 | 35 |
35 GIFImageDecoder::GIFImageDecoder(AlphaOption alpha_option, | 36 GIFImageDecoder::GIFImageDecoder(AlphaOption alpha_option, |
36 const ColorBehavior& color_behavior, | 37 const ColorBehavior& color_behavior, |
37 size_t max_decoded_bytes) | 38 size_t max_decoded_bytes) |
38 : ImageDecoder(alpha_option, color_behavior, max_decoded_bytes), | 39 : ImageDecoder(alpha_option, color_behavior, max_decoded_bytes), |
39 repetition_count_(kAnimationLoopOnce) {} | 40 codec_(), |
40 | 41 segment_stream_(nullptr) {} |
41 GIFImageDecoder::~GIFImageDecoder() {} | 42 |
43 GIFImageDecoder::~GIFImageDecoder() = default; | |
42 | 44 |
43 void GIFImageDecoder::OnSetData(SegmentReader* data) { | 45 void GIFImageDecoder::OnSetData(SegmentReader* data) { |
44 if (reader_) | 46 if (!data) { |
45 reader_->SetData(data); | 47 if (segment_stream_) |
48 segment_stream_->SetReader(nullptr); | |
49 return; | |
50 } | |
51 | |
52 std::unique_ptr<SegmentStream> segment_stream; | |
53 if (!segment_stream_) { | |
54 segment_stream = base::MakeUnique<SegmentStream>(); | |
55 segment_stream_ = segment_stream.get(); | |
56 } | |
57 | |
58 segment_stream_->SetReader(PassRefPtr<SegmentReader>(data)); | |
scroggo_chromium
2017/08/15 15:19:44
I think PassRefPtr is deprecated?
https://docs.go
cblume
2017/08/15 18:03:34
Done.
| |
59 | |
60 if (!codec_) { | |
61 SkCodec::Result codec_creation_result; | |
62 codec_ = SkCodec::MakeFromStream(std::move(segment_stream), | |
63 &codec_creation_result, nullptr); | |
64 switch (codec_creation_result) { | |
65 case SkCodec::kSuccess: { | |
66 // SkCodec::MakeFromStream will read enough of the image to get the | |
67 // image size. | |
68 SkImageInfo image_info = codec_->getInfo(); | |
69 SetSize(image_info.width(), image_info.height()); | |
70 return; | |
71 } | |
72 case SkCodec::kIncompleteInput: | |
73 // |segment_stream_|'s ownership is passed into MakeFromStream. | |
74 // It is deleted if MakeFromStream fails. | |
75 // If MakeFromStream fails, we set |segment_stream_| to null so | |
76 // we aren't pointing to reclaimed memory. | |
77 segment_stream_ = nullptr; | |
78 return; | |
79 default: | |
80 SetFailed(); | |
81 return; | |
82 } | |
83 } | |
46 } | 84 } |
47 | 85 |
48 int GIFImageDecoder::RepetitionCount() const { | 86 int GIFImageDecoder::RepetitionCount() const { |
87 if (!codec_ || segment_stream_->IsCleared()) | |
88 return repetition_count_; | |
89 | |
90 DCHECK(!Failed()); | |
91 | |
49 // This value can arrive at any point in the image data stream. Most GIFs | 92 // This value can arrive at any point in the image data stream. Most GIFs |
50 // in the wild declare it near the beginning of the file, so it usually is | 93 // in the wild declare it near the beginning of the file, so it usually is |
51 // set by the time we've decoded the size, but (depending on the GIF and the | 94 // set by the time we've decoded the size, but (depending on the GIF and the |
52 // packets sent back by the webserver) not always. If the reader hasn't | 95 // packets sent back by the webserver) not always. |
53 // seen a loop count yet, it will return kCLoopCountNotSeen, in which case we | |
54 // should default to looping once (the initial value for | |
55 // |repetition_count_|). | |
56 // | 96 // |
57 // There are some additional wrinkles here. First, ImageSource::Clear() | 97 // SkCodec will parse forward in the file if the repetition count has not |
58 // may destroy the reader, making the result from the reader _less_ | 98 // been seen yet. |
59 // authoritative on future calls if the recreated reader hasn't seen the | 99 int repetition_count = codec_->getRepetitionCount(); |
60 // loop count. We don't need to special-case this because in this case the | 100 |
61 // new reader will once again return kCLoopCountNotSeen, and we won't | 101 switch (repetition_count) { |
62 // overwrite the cached correct value. | 102 case 0: { |
103 // SkCodec returns 0 for both still images and animated images which | |
104 // only play once. | |
105 if (IsAllDataReceived() && codec_->getFrameCount() == 1) { | |
106 repetition_count_ = kAnimationNone; | |
107 break; | |
108 } | |
109 | |
110 repetition_count_ = kAnimationLoopOnce; | |
111 break; | |
112 } | |
113 case SkCodec::kRepetitionCountInfinite: | |
114 repetition_count_ = kAnimationLoopInfinite; | |
115 break; | |
116 default: | |
117 repetition_count_ = repetition_count; | |
118 break; | |
119 } | |
120 | |
121 return repetition_count_; | |
122 } | |
123 | |
124 bool GIFImageDecoder::FrameIsReceivedAtIndex(size_t index) const { | |
125 SkCodec::FrameInfo frame_info; | |
126 if (!codec_ || !codec_->getFrameInfo(index, &frame_info)) | |
127 return false; | |
128 return frame_info.fFullyReceived; | |
129 } | |
130 | |
131 float GIFImageDecoder::FrameDurationAtIndex(size_t index) const { | |
132 if (index < frame_buffer_cache_.size()) | |
133 return frame_buffer_cache_[index].Duration(); | |
134 return 0; | |
135 } | |
136 | |
137 bool GIFImageDecoder::SetFailed() { | |
138 segment_stream_ = nullptr; | |
139 codec_.reset(); | |
140 return ImageDecoder::SetFailed(); | |
141 } | |
142 | |
143 size_t GIFImageDecoder::ClearCacheExceptFrame(size_t index) { | |
144 // SkCodec attempts to report the earliest possible required frame, but it is | |
145 // possible that frame has been evicted, while a later frame (which could also | |
146 // be used as the required frame) is still cached. Try to preserve a frame | |
147 // that is still cached. | |
148 if (frame_buffer_cache_.size() <= 1) | |
149 return 0; | |
150 | |
151 size_t index2 = kNotFound; | |
152 if (index < frame_buffer_cache_.size()) { | |
153 const ImageFrame& frame = frame_buffer_cache_[index]; | |
154 if (!FrameStatusSufficientForSuccessors(index) || | |
155 frame.GetDisposalMethod() == ImageFrame::kDisposeOverwritePrevious) { | |
156 index2 = GetViableReferenceFrameIndex(index); | |
157 } | |
158 } | |
159 | |
160 return ClearCacheExceptTwoFrames(index, index2); | |
161 } | |
162 | |
163 size_t GIFImageDecoder::DecodeFrameCount() { | |
164 if (!codec_ || segment_stream_->IsCleared()) | |
165 return frame_buffer_cache_.size(); | |
166 | |
167 return codec_->getFrameCount(); | |
168 } | |
169 | |
170 void GIFImageDecoder::InitializeNewFrame(size_t index) { | |
171 DCHECK(codec_); | |
172 | |
173 ImageFrame& frame = frame_buffer_cache_[index]; | |
174 // SkCodec does not inform us if only a portion of the image was updated | |
175 // in the current frame. Because of this, rather than correctly filling in | |
176 // the frame rect, we set the frame rect to be the image's full size. | |
177 // The original frame rect is not used, anyway. | |
178 IntSize full_image_size = Size(); | |
179 frame.SetOriginalFrameRect(IntRect(IntPoint(), full_image_size)); | |
180 | |
181 SkCodec::FrameInfo frame_info; | |
182 bool frame_info_received = codec_->getFrameInfo(index, &frame_info); | |
183 DCHECK(frame_info_received); | |
184 frame.SetDuration(frame_info.fDuration); | |
185 size_t required_previous_frame_index; | |
186 if (frame_info.fRequiredFrame == SkCodec::kNone) { | |
187 required_previous_frame_index = kNotFound; | |
188 } else { | |
189 required_previous_frame_index = | |
190 static_cast<size_t>(frame_info.fRequiredFrame); | |
191 } | |
192 frame.SetRequiredPreviousFrameIndex(required_previous_frame_index); | |
193 | |
194 ImageFrame::DisposalMethod disposal_method = ImageFrame::kDisposeNotSpecified; | |
195 switch (frame_info.fDisposalMethod) { | |
196 case SkCodecAnimation::DisposalMethod::kKeep: | |
197 disposal_method = ImageFrame::kDisposeKeep; | |
198 break; | |
199 case SkCodecAnimation::DisposalMethod::kRestoreBGColor: | |
200 disposal_method = ImageFrame::kDisposeOverwriteBgcolor; | |
201 break; | |
202 case SkCodecAnimation::DisposalMethod::kRestorePrevious: | |
203 disposal_method = ImageFrame::kDisposeOverwritePrevious; | |
204 break; | |
205 } | |
206 frame.SetDisposalMethod(disposal_method); | |
207 } | |
208 | |
209 void GIFImageDecoder::Decode(size_t index) { | |
210 if (!codec_ || segment_stream_->IsCleared()) | |
211 return; | |
212 | |
213 DCHECK(!Failed()); | |
214 | |
215 DCHECK_LT(index, frame_buffer_cache_.size()); | |
216 | |
217 UpdateAggressivePurging(index); | |
218 SkImageInfo image_info = codec_->getInfo() | |
219 .makeColorType(kN32_SkColorType) | |
220 .makeColorSpace(ColorSpaceForSkImages()); | |
221 | |
222 SkCodec::Options options; | |
223 options.fFrameIndex = index; | |
224 options.fPriorFrame = SkCodec::kNone; | |
225 options.fZeroInitialized = SkCodec::kNo_ZeroInitialized; | |
226 | |
227 ImageFrame& frame = frame_buffer_cache_[index]; | |
228 if (frame.GetStatus() == ImageFrame::kFrameEmpty) { | |
229 size_t required_previous_frame_index = frame.RequiredPreviousFrameIndex(); | |
230 if (required_previous_frame_index == kNotFound) { | |
231 frame.AllocatePixelData(Size().Width(), Size().Height(), | |
232 ColorSpaceForSkImages()); | |
233 frame.ZeroFillPixelData(); | |
234 } else { | |
235 size_t previous_frame_index = GetViableReferenceFrameIndex(index); | |
236 if (previous_frame_index == kNotFound) { | |
237 previous_frame_index = required_previous_frame_index; | |
238 Decode(previous_frame_index); | |
239 } | |
240 | |
241 // We try to reuse |previous_frame| as starting state to avoid copying. | |
242 // If CanReusePreviousFrameBuffer returns false, we must copy the data | |
243 // since |previous_frame| is necessary to decode this or later frames. | |
244 // In that case copy the data instead. | |
245 ImageFrame& previous_frame = frame_buffer_cache_[previous_frame_index]; | |
246 if ((!CanReusePreviousFrameBuffer(index) || | |
247 !frame.TakeBitmapDataIfWritable(&previous_frame)) && | |
248 !frame.CopyBitmapData(previous_frame)) { | |
249 SetFailed(); | |
250 return; | |
251 } | |
252 options.fPriorFrame = previous_frame_index; | |
253 } | |
254 } | |
255 | |
256 if (frame.GetStatus() == ImageFrame::kFrameAllocated) { | |
257 SkCodec::Result start_incremental_decode_result = | |
258 codec_->startIncrementalDecode(image_info, frame.Bitmap().getPixels(), | |
259 frame.Bitmap().rowBytes(), &options); | |
260 switch (start_incremental_decode_result) { | |
261 case SkCodec::kSuccess: | |
262 break; | |
263 case SkCodec::kIncompleteInput: | |
264 return; | |
265 default: | |
266 SetFailed(); | |
267 return; | |
268 } | |
269 frame.SetStatus(ImageFrame::kFramePartial); | |
270 } | |
271 | |
272 SkCodec::Result incremental_decode_result = codec_->incrementalDecode(); | |
273 switch (incremental_decode_result) { | |
274 case SkCodec::kSuccess: { | |
275 SkCodec::FrameInfo frame_info; | |
276 bool frame_info_received = codec_->getFrameInfo(index, &frame_info); | |
277 DCHECK(frame_info_received); | |
278 frame.SetHasAlpha(!SkAlphaTypeIsOpaque(frame_info.fAlphaType)); | |
279 frame.SetPixelsChanged(true); | |
280 frame.SetStatus(ImageFrame::kFrameComplete); | |
281 PostDecodeProcessing(index); | |
282 break; | |
283 } | |
284 case SkCodec::kIncompleteInput: | |
285 frame.SetPixelsChanged(true); | |
286 if (FrameIsReceivedAtIndex(index) || IsAllDataReceived()) { | |
287 SetFailed(); | |
288 } | |
289 break; | |
290 default: | |
291 SetFailed(); | |
292 break; | |
293 } | |
294 } | |
295 | |
296 bool GIFImageDecoder::CanReusePreviousFrameBuffer(size_t index) const { | |
297 DCHECK_LT(index, frame_buffer_cache_.size()); | |
298 return frame_buffer_cache_[index].GetDisposalMethod() != | |
299 ImageFrame::kDisposeOverwritePrevious; | |
300 } | |
301 | |
302 size_t GIFImageDecoder::GetViableReferenceFrameIndex( | |
303 size_t dependent_index) const { | |
304 DCHECK_LT(dependent_index, frame_buffer_cache_.size()); | |
305 | |
306 size_t required_previous_frame_index = | |
307 frame_buffer_cache_[dependent_index].RequiredPreviousFrameIndex(); | |
308 | |
309 // Any frame in the range [|required_previous_frame_index|, |dependent_index|) | |
310 // which has a disposal method other than kRestorePrevious can be provided as | |
311 // the prior frame to SkCodec. | |
63 // | 312 // |
64 // Second, a GIF might never set a loop count at all, in which case we | 313 // SkCodec sets SkCodec::FrameInfo::fRequiredFrame to the earliest frame which |
65 // should continue to treat it as a "loop once" animation. We don't need | 314 // can be used. This might come up when several frames update the same |
66 // special code here either, because in this case we'll never change | 315 // subregion. If that same subregion is about to be overwritten, it doesn't |
67 // |repetition_count_| from its default value. | 316 // matter which frame in that chain is provided. |
68 // | 317 size_t previous_reference_frame_index = kNotFound; |
69 // Third, we use the same GIFImageReader for counting frames and we might | 318 if (required_previous_frame_index != kNotFound) { |
scroggo_chromium
2017/08/15 15:19:44
This is checking to see if the frame is independen
cblume
2017/08/15 18:03:33
Done.
| |
70 // see the loop count and then encounter a decoding error which happens | 319 // Loop backwards because the frames most likely to be in cache are the most |
71 // later in the stream. It is also possible that no frames are in the | 320 // recent. |
72 // stream. In these cases we should just loop once. | 321 for (size_t i = dependent_index - 1; i != required_previous_frame_index; |
73 if (IsAllDataReceived() && ParseCompleted() && reader_->ImagesCount() == 1) | 322 i--) { |
74 repetition_count_ = kAnimationNone; | 323 const ImageFrame& frame = frame_buffer_cache_[i]; |
75 else if (Failed() || (reader_ && (!reader_->ImagesCount()))) | 324 |
76 repetition_count_ = kAnimationLoopOnce; | 325 if (frame.GetDisposalMethod() == ImageFrame::kDisposeOverwritePrevious) |
77 else if (reader_ && reader_->LoopCount() != kCLoopCountNotSeen) | 326 continue; |
78 repetition_count_ = reader_->LoopCount(); | 327 |
79 return repetition_count_; | 328 if (frame.GetStatus() == ImageFrame::kFrameComplete) { |
80 } | 329 previous_reference_frame_index = i; |
81 | 330 break; |
82 bool GIFImageDecoder::FrameIsReceivedAtIndex(size_t index) const { | 331 } |
83 return reader_ && (index < reader_->ImagesCount()) && | 332 } |
84 reader_->FrameContext(index)->IsComplete(); | 333 } |
85 } | 334 |
86 | 335 return previous_reference_frame_index; |
87 float GIFImageDecoder::FrameDurationAtIndex(size_t index) const { | |
88 return (reader_ && (index < reader_->ImagesCount()) && | |
89 reader_->FrameContext(index)->IsHeaderDefined()) | |
90 ? reader_->FrameContext(index)->DelayTime() | |
91 : 0; | |
92 } | |
93 | |
94 bool GIFImageDecoder::SetFailed() { | |
95 reader_.reset(); | |
96 return ImageDecoder::SetFailed(); | |
97 } | |
98 | |
99 bool GIFImageDecoder::HaveDecodedRow(size_t frame_index, | |
100 GIFRow::const_iterator row_begin, | |
101 size_t width, | |
102 size_t row_number, | |
103 unsigned repeat_count, | |
104 bool write_transparent_pixels) { | |
105 const GIFFrameContext* frame_context = reader_->FrameContext(frame_index); | |
106 // The pixel data and coordinates supplied to us are relative to the frame's | |
107 // origin within the entire image size, i.e. | |
108 // (frameC_context->xOffset, frame_context->yOffset). There is no guarantee | |
109 // that width == (size().width() - frame_context->xOffset), so | |
110 // we must ensure we don't run off the end of either the source data or the | |
111 // row's X-coordinates. | |
112 const int x_begin = frame_context->XOffset(); | |
113 const int y_begin = frame_context->YOffset() + row_number; | |
114 const int x_end = std::min(static_cast<int>(frame_context->XOffset() + width), | |
115 Size().Width()); | |
116 const int y_end = std::min( | |
117 static_cast<int>(frame_context->YOffset() + row_number + repeat_count), | |
118 Size().Height()); | |
119 if (!width || (x_begin < 0) || (y_begin < 0) || (x_end <= x_begin) || | |
120 (y_end <= y_begin)) | |
121 return true; | |
122 | |
123 const GIFColorMap::Table& color_table = | |
124 frame_context->LocalColorMap().IsDefined() | |
125 ? frame_context->LocalColorMap().GetTable() | |
126 : reader_->GlobalColorMap().GetTable(); | |
127 | |
128 if (color_table.IsEmpty()) | |
129 return true; | |
130 | |
131 GIFColorMap::Table::const_iterator color_table_iter = color_table.begin(); | |
132 | |
133 // Initialize the frame if necessary. | |
134 ImageFrame& buffer = frame_buffer_cache_[frame_index]; | |
135 if (!InitFrameBuffer(frame_index)) | |
136 return false; | |
137 | |
138 const size_t transparent_pixel = frame_context->TransparentPixel(); | |
139 GIFRow::const_iterator row_end = row_begin + (x_end - x_begin); | |
140 ImageFrame::PixelData* current_address = buffer.GetAddr(x_begin, y_begin); | |
141 | |
142 // We may or may not need to write transparent pixels to the buffer. | |
143 // If we're compositing against a previous image, it's wrong, and if | |
144 // we're writing atop a cleared, fully transparent buffer, it's | |
145 // unnecessary; but if we're decoding an interlaced gif and | |
146 // displaying it "Haeberli"-style, we must write these for passes | |
147 // beyond the first, or the initial passes will "show through" the | |
148 // later ones. | |
149 // | |
150 // The loops below are almost identical. One writes a transparent pixel | |
151 // and one doesn't based on the value of |write_transparent_pixels|. | |
152 // The condition check is taken out of the loop to enhance performance. | |
153 // This optimization reduces decoding time by about 15% for a 3MB image. | |
154 if (write_transparent_pixels) { | |
155 for (; row_begin != row_end; ++row_begin, ++current_address) { | |
156 const size_t source_value = *row_begin; | |
157 if ((source_value != transparent_pixel) && | |
158 (source_value < color_table.size())) { | |
159 *current_address = color_table_iter[source_value]; | |
160 } else { | |
161 *current_address = 0; | |
162 current_buffer_saw_alpha_ = true; | |
163 } | |
164 } | |
165 } else { | |
166 for (; row_begin != row_end; ++row_begin, ++current_address) { | |
167 const size_t source_value = *row_begin; | |
168 if ((source_value != transparent_pixel) && | |
169 (source_value < color_table.size())) | |
170 *current_address = color_table_iter[source_value]; | |
171 else | |
172 current_buffer_saw_alpha_ = true; | |
173 } | |
174 } | |
175 | |
176 // Tell the frame to copy the row data if need be. | |
177 if (repeat_count > 1) | |
178 buffer.CopyRowNTimes(x_begin, x_end, y_begin, y_end); | |
179 | |
180 buffer.SetPixelsChanged(true); | |
181 return true; | |
182 } | |
183 | |
184 bool GIFImageDecoder::ParseCompleted() const { | |
185 return reader_ && reader_->ParseCompleted(); | |
186 } | |
187 | |
188 bool GIFImageDecoder::FrameComplete(size_t frame_index) { | |
189 // Initialize the frame if necessary. Some GIFs insert do-nothing frames, | |
190 // in which case we never reach HaveDecodedRow() before getting here. | |
191 if (!InitFrameBuffer(frame_index)) | |
192 return SetFailed(); | |
193 | |
194 if (!current_buffer_saw_alpha_) | |
195 CorrectAlphaWhenFrameBufferSawNoAlpha(frame_index); | |
196 | |
197 frame_buffer_cache_[frame_index].SetStatus(ImageFrame::kFrameComplete); | |
198 | |
199 return true; | |
200 } | |
201 | |
202 void GIFImageDecoder::ClearFrameBuffer(size_t frame_index) { | |
203 if (reader_ && frame_buffer_cache_[frame_index].GetStatus() == | |
204 ImageFrame::kFramePartial) { | |
205 // Reset the state of the partial frame in the reader so that the frame | |
206 // can be decoded again when requested. | |
207 reader_->ClearDecodeState(frame_index); | |
208 } | |
209 ImageDecoder::ClearFrameBuffer(frame_index); | |
210 } | |
211 | |
212 size_t GIFImageDecoder::DecodeFrameCount() { | |
213 Parse(kGIFFrameCountQuery); | |
214 // If decoding fails, |reader_| will have been destroyed. Instead of | |
215 // returning 0 in this case, return the existing number of frames. This way | |
216 // if we get halfway through the image before decoding fails, we won't | |
217 // suddenly start reporting that the image has zero frames. | |
218 return Failed() ? frame_buffer_cache_.size() : reader_->ImagesCount(); | |
219 } | |
220 | |
221 void GIFImageDecoder::InitializeNewFrame(size_t index) { | |
222 ImageFrame* buffer = &frame_buffer_cache_[index]; | |
223 const GIFFrameContext* frame_context = reader_->FrameContext(index); | |
224 buffer->SetOriginalFrameRect( | |
225 Intersection(frame_context->FrameRect(), IntRect(IntPoint(), Size()))); | |
226 buffer->SetDuration(frame_context->DelayTime()); | |
227 buffer->SetDisposalMethod(frame_context->GetDisposalMethod()); | |
228 buffer->SetRequiredPreviousFrameIndex( | |
229 FindRequiredPreviousFrame(index, false)); | |
230 } | |
231 | |
232 void GIFImageDecoder::Decode(size_t index) { | |
233 Parse(kGIFFrameCountQuery); | |
234 | |
235 if (Failed()) | |
236 return; | |
237 | |
238 UpdateAggressivePurging(index); | |
239 | |
240 Vector<size_t> frames_to_decode = FindFramesToDecode(index); | |
241 for (auto i = frames_to_decode.rbegin(); i != frames_to_decode.rend(); ++i) { | |
242 if (!reader_->Decode(*i)) { | |
243 SetFailed(); | |
244 return; | |
245 } | |
246 | |
247 // If this returns false, we need more data to continue decoding. | |
248 if (!PostDecodeProcessing(*i)) | |
249 break; | |
250 } | |
251 | |
252 // It is also a fatal error if all data is received and we have decoded all | |
253 // frames available but the file is truncated. | |
254 if (index >= frame_buffer_cache_.size() - 1 && IsAllDataReceived() && | |
255 reader_ && !reader_->ParseCompleted()) | |
256 SetFailed(); | |
257 } | |
258 | |
259 void GIFImageDecoder::Parse(GIFParseQuery query) { | |
260 if (Failed()) | |
261 return; | |
262 | |
263 if (!reader_) { | |
264 reader_ = WTF::MakeUnique<GIFImageReader>(this); | |
265 reader_->SetData(data_); | |
266 } | |
267 | |
268 if (!reader_->Parse(query)) | |
269 SetFailed(); | |
270 } | |
271 | |
272 void GIFImageDecoder::OnInitFrameBuffer(size_t frame_index) { | |
273 current_buffer_saw_alpha_ = false; | |
274 } | |
275 | |
276 bool GIFImageDecoder::CanReusePreviousFrameBuffer(size_t frame_index) const { | |
277 DCHECK(frame_index < frame_buffer_cache_.size()); | |
278 return frame_buffer_cache_[frame_index].GetDisposalMethod() != | |
279 ImageFrame::kDisposeOverwritePrevious; | |
280 } | 336 } |
281 | 337 |
282 } // namespace blink | 338 } // namespace blink |
OLD | NEW |