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" | |
30 #include "platform/wtf/NotFound.h" | 29 #include "platform/wtf/NotFound.h" |
31 #include "platform/wtf/PtrUtil.h" | 30 #include "platform/wtf/PtrUtil.h" |
31 #include "third_party/skia/include/core/SkImageInfo.h" | |
32 | 32 |
33 namespace blink { | 33 namespace blink { |
34 | 34 |
35 GIFImageDecoder::GIFImageDecoder(AlphaOption alpha_option, | 35 GIFImageDecoder::GIFImageDecoder(AlphaOption alpha_option, |
36 const ColorBehavior& color_behavior, | 36 const ColorBehavior& color_behavior, |
37 size_t max_decoded_bytes) | 37 size_t max_decoded_bytes) |
38 : ImageDecoder(alpha_option, color_behavior, max_decoded_bytes), | 38 : ImageDecoder(alpha_option, color_behavior, max_decoded_bytes), |
39 repetition_count_(kAnimationLoopOnce) {} | 39 codec_(), |
40 | 40 segment_stream_(nullptr), |
41 GIFImageDecoder::~GIFImageDecoder() {} | 41 frame_infos_() {} |
42 | |
43 GIFImageDecoder::~GIFImageDecoder() { | |
44 if (!codec_) { | |
45 // if we did not create |codec_| and thus did not pass ownership to it | |
46 if (segment_stream_) | |
47 delete segment_stream_; | |
48 } | |
49 } | |
42 | 50 |
43 void GIFImageDecoder::OnSetData(SegmentReader* data) { | 51 void GIFImageDecoder::OnSetData(SegmentReader* data) { |
44 if (reader_) | 52 if (!data) { |
45 reader_->SetData(data); | 53 if (segment_stream_) |
54 segment_stream_->SetReader(nullptr); | |
55 return; | |
56 } | |
57 | |
58 if (!segment_stream_) | |
59 segment_stream_ = new SegmentStream(); | |
60 | |
61 segment_stream_->SetReader(PassRefPtr<SegmentReader>(data)); | |
62 | |
63 if (!codec_) { | |
64 codec_.reset(SkCodec::NewFromStream(segment_stream_, nullptr)); | |
65 if (!codec_) { | |
66 // |segment_stream_|'s ownership is passed into NewFromStream. | |
67 // It is deleted if NewFromStream fails. | |
68 // If NewFromStream fails, we set |segment_stream_| to null so | |
69 // we aren't pointing to reclaimed memory. | |
70 segment_stream_ = nullptr; | |
71 return; | |
72 } | |
73 | |
74 // SkCodec::NewFromStream will read enough of the image to get the image | |
75 // size. | |
76 SkImageInfo image_info = codec_->getInfo(); | |
77 SetSize(image_info.width(), image_info.height()); | |
78 } | |
46 } | 79 } |
47 | 80 |
48 int GIFImageDecoder::RepetitionCount() const { | 81 int GIFImageDecoder::RepetitionCount() const { |
82 if (!codec_) | |
83 return kAnimationLoopOnce; | |
84 | |
85 DCHECK(!Failed()); | |
86 | |
49 // This value can arrive at any point in the image data stream. Most GIFs | 87 // 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 | 88 // 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 | 89 // 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 | 90 // 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 // | 91 // |
57 // There are some additional wrinkles here. First, ImageSource::Clear() | 92 // SkCodec will parse forward in the file if the repetition count has not been |
58 // may destroy the reader, making the result from the reader _less_ | 93 // seen yet. |
59 // authoritative on future calls if the recreated reader hasn't seen the | 94 int repetition_count = codec_->getRepetitionCount(); |
60 // loop count. We don't need to special-case this because in this case the | 95 |
61 // new reader will once again return kCLoopCountNotSeen, and we won't | 96 switch (repetition_count) { |
62 // overwrite the cached correct value. | 97 case 0: { |
63 // | 98 size_t frame_count = codec_->getFrameCount(); |
64 // Second, a GIF might never set a loop count at all, in which case we | 99 if (IsAllDataReceived() && frame_count == 1) |
65 // should continue to treat it as a "loop once" animation. We don't need | 100 return kAnimationNone; |
66 // special code here either, because in this case we'll never change | 101 |
67 // |repetition_count_| from its default value. | 102 return kAnimationLoopOnce; |
68 // | 103 } |
69 // Third, we use the same GIFImageReader for counting frames and we might | 104 case SkCodec::kRepetitionCountInfinite: |
70 // see the loop count and then encounter a decoding error which happens | 105 return kAnimationLoopInfinite; |
71 // later in the stream. It is also possible that no frames are in the | 106 default: |
72 // stream. In these cases we should just loop once. | 107 return repetition_count; |
73 if (IsAllDataReceived() && ParseCompleted() && reader_->ImagesCount() == 1) | 108 } |
74 repetition_count_ = kAnimationNone; | |
75 else if (Failed() || (reader_ && (!reader_->ImagesCount()))) | |
76 repetition_count_ = kAnimationLoopOnce; | |
77 else if (reader_ && reader_->LoopCount() != kCLoopCountNotSeen) | |
78 repetition_count_ = reader_->LoopCount(); | |
79 return repetition_count_; | |
80 } | 109 } |
81 | 110 |
82 bool GIFImageDecoder::FrameIsCompleteAtIndex(size_t index) const { | 111 bool GIFImageDecoder::FrameIsCompleteAtIndex(size_t index) const { |
83 return reader_ && (index < reader_->ImagesCount()) && | 112 if (!codec_) |
84 reader_->FrameContext(index)->IsComplete(); | 113 return false; |
114 | |
115 if (frame_infos_.size() <= index) | |
116 return false; | |
117 | |
118 return frame_infos_[index].fFullyReceived; | |
85 } | 119 } |
86 | 120 |
87 float GIFImageDecoder::FrameDurationAtIndex(size_t index) const { | 121 float GIFImageDecoder::FrameDurationAtIndex(size_t index) const { |
88 return (reader_ && (index < reader_->ImagesCount()) && | 122 if (index < frame_buffer_cache_.size()) |
89 reader_->FrameContext(index)->IsHeaderDefined()) | 123 return frame_buffer_cache_[index].Duration(); |
90 ? reader_->FrameContext(index)->DelayTime() | 124 return 0; |
91 : 0; | |
92 } | 125 } |
93 | 126 |
94 bool GIFImageDecoder::SetFailed() { | 127 bool GIFImageDecoder::SetFailed() { |
95 reader_.reset(); | 128 DCHECK(codec_); |
129 | |
130 segment_stream_ = nullptr; | |
131 codec_ = nullptr; | |
96 return ImageDecoder::SetFailed(); | 132 return ImageDecoder::SetFailed(); |
97 } | 133 } |
98 | 134 |
99 bool GIFImageDecoder::HaveDecodedRow(size_t frame_index, | 135 size_t GIFImageDecoder::DecodeFrameCount() { |
100 GIFRow::const_iterator row_begin, | 136 if (!codec_) |
101 size_t width, | 137 return 0; |
102 size_t row_number, | 138 |
103 unsigned repeat_count, | 139 if (!Failed() && !segment_stream_->IsCleared()) |
104 bool write_transparent_pixels) { | 140 frame_infos_ = codec_->getFrameInfo(); |
scroggo_chromium
2017/06/05 19:00:53
We talked about calling codec_->getFrameCount() an
cblume
2017/06/06 04:04:35
Oh, I missed that patch to SkCodec::getFrameInfo()
| |
105 const GIFFrameContext* frame_context = reader_->FrameContext(frame_index); | 141 return frame_infos_.size(); |
106 // The pixel data and coordinates supplied to us are relative to the frame's | 142 } |
107 // origin within the entire image size, i.e. | 143 |
108 // (frameC_context->xOffset, frame_context->yOffset). There is no guarantee | 144 void GIFImageDecoder::InitializeNewFrame(size_t index) { |
109 // that width == (size().width() - frame_context->xOffset), so | 145 DCHECK(codec_); |
110 // we must ensure we don't run off the end of either the source data or the | 146 |
111 // row's X-coordinates. | 147 ImageFrame& frame = frame_buffer_cache_[index]; |
112 const int x_begin = frame_context->XOffset(); | 148 // SkCodec does not inform us if only a portion of the image was updated |
113 const int y_begin = frame_context->YOffset() + row_number; | 149 // in the current frame. Because of this, rather than correctly filling in |
114 const int x_end = std::min(static_cast<int>(frame_context->XOffset() + width), | 150 // the frame rect, we set the frame rect to be the image's full size. |
115 Size().Width()); | 151 // The original frame rect is not used, anyway. |
116 const int y_end = std::min( | 152 IntSize full_image_size = Size(); |
117 static_cast<int>(frame_context->YOffset() + row_number + repeat_count), | 153 frame.SetOriginalFrameRect(IntRect(IntPoint(), full_image_size)); |
118 Size().Height()); | 154 frame.SetDuration(frame_infos_[index].fDuration); |
119 if (!width || (x_begin < 0) || (y_begin < 0) || (x_end <= x_begin) || | 155 frame.SetHasAlpha(!SkAlphaTypeIsOpaque(frame_infos_[index].fAlphaType)); |
120 (y_end <= y_begin)) | 156 size_t required_previous_frame_index; |
121 return true; | 157 if (frame_infos_[index].fRequiredFrame == SkCodec::kNone) { |
122 | 158 required_previous_frame_index = WTF::kNotFound; |
123 const GIFColorMap::Table& color_table = | 159 } else { |
124 frame_context->LocalColorMap().IsDefined() | 160 required_previous_frame_index = |
125 ? frame_context->LocalColorMap().GetTable() | 161 static_cast<size_t>(frame_infos_[index].fRequiredFrame); |
126 : reader_->GlobalColorMap().GetTable(); | 162 } |
127 | 163 frame.SetRequiredPreviousFrameIndex(required_previous_frame_index); |
128 if (color_table.IsEmpty()) | 164 // The disposal method is not required any more, but is left in place |
scroggo_chromium
2017/06/05 19:00:53
Sorry, I still need to add this to SkCodec.
cblume
2017/06/06 04:04:35
No problem.
Even without it, fRequiredFrame allows
| |
129 return true; | 165 // for the other image decoders that do not yet rely on SkCodec. |
130 | 166 // For now, fill it with DisposeKeep. |
131 GIFColorMap::Table::const_iterator color_table_iter = color_table.begin(); | 167 frame.SetDisposalMethod(ImageFrame::kDisposeKeep); |
132 | 168 } |
133 // Initialize the frame if necessary. | 169 |
134 ImageFrame& buffer = frame_buffer_cache_[frame_index]; | 170 void GIFImageDecoder::Decode(size_t index) { |
135 if (!InitFrameBuffer(frame_index)) | 171 if (!codec_) |
136 return false; | 172 return; |
137 | 173 |
138 const size_t transparent_pixel = frame_context->TransparentPixel(); | 174 DCHECK(!Failed()); |
139 GIFRow::const_iterator row_end = row_begin + (x_end - x_begin); | 175 |
140 ImageFrame::PixelData* current_address = buffer.GetAddr(x_begin, y_begin); | 176 DCHECK_LT(index, frame_buffer_cache_.size()); |
141 | 177 |
142 // We may or may not need to write transparent pixels to the buffer. | 178 if (segment_stream_->IsCleared()) |
143 // If we're compositing against a previous image, it's wrong, and if | 179 return; |
144 // we're writing atop a cleared, fully transparent buffer, it's | 180 |
145 // unnecessary; but if we're decoding an interlaced gif and | 181 UpdateAggressivePurging(index); |
146 // displaying it "Haeberli"-style, we must write these for passes | 182 SkImageInfo image_info = codec_->getInfo().makeColorType(kN32_SkColorType); |
scroggo_chromium
2017/06/05 19:00:53
I think you also want to modify this to use ColorS
cblume
2017/06/06 04:04:36
Done.
| |
147 // beyond the first, or the initial passes will "show through" the | 183 |
148 // later ones. | 184 SkCodec::Options options; |
149 // | 185 options.fFrameIndex = index; |
150 // The loops below are almost identical. One writes a transparent pixel | 186 options.fHasPriorFrame = false; |
151 // and one doesn't based on the value of |write_transparent_pixels|. | 187 options.fZeroInitialized = SkCodec::kNo_ZeroInitialized; |
152 // The condition check is taken out of the loop to enhance performance. | 188 |
153 // This optimization reduces decoding time by about 15% for a 3MB image. | 189 ImageFrame& frame = frame_buffer_cache_[index]; |
154 if (write_transparent_pixels) { | 190 if (frame.GetStatus() == ImageFrame::kFrameEmpty) { |
155 for (; row_begin != row_end; ++row_begin, ++current_address) { | 191 size_t required_previous_frame_index = frame.RequiredPreviousFrameIndex(); |
156 const size_t source_value = *row_begin; | 192 if (required_previous_frame_index == WTF::kNotFound) { |
157 if ((source_value != transparent_pixel) && | 193 frame.AllocatePixelData(Size().Width(), Size().Height(), |
158 (source_value < color_table.size())) { | 194 ColorSpaceForSkImages()); |
159 *current_address = color_table_iter[source_value]; | 195 } else { |
160 } else { | 196 ImageFrame& required_previous_frame = |
161 *current_address = 0; | 197 frame_buffer_cache_[required_previous_frame_index]; |
162 current_buffer_saw_alpha_ = true; | 198 |
199 if (required_previous_frame.GetStatus() != ImageFrame::kFrameComplete) | |
200 Decode(required_previous_frame_index); | |
201 | |
202 // We try to reuse |required_previous_frame| as starting state to avoid | |
203 // copying. If CanReusePreviousFrameBuffer returns false, we must copy | |
204 // the data since |required_previous_frame| is necessary to decode this | |
205 // or later frames. In that case copy the data instead. | |
206 if ((!CanReusePreviousFrameBuffer(index) || | |
207 !frame.TakeBitmapDataIfWritable(&required_previous_frame)) && | |
208 !frame.CopyBitmapData(required_previous_frame)) { | |
209 SetFailed(); | |
210 return; | |
163 } | 211 } |
164 } | 212 options.fHasPriorFrame = true; |
165 } else { | 213 } |
166 for (; row_begin != row_end; ++row_begin, ++current_address) { | 214 } |
167 const size_t source_value = *row_begin; | 215 |
168 if ((source_value != transparent_pixel) && | 216 if (frame.GetStatus() == ImageFrame::kFrameAllocated) { |
169 (source_value < color_table.size())) | 217 SkCodec::Result start_incremental_decode_result = |
170 *current_address = color_table_iter[source_value]; | 218 codec_->startIncrementalDecode(image_info, frame.Bitmap().getPixels(), |
171 else | 219 frame.Bitmap().rowBytes(), &options, |
172 current_buffer_saw_alpha_ = true; | 220 nullptr, nullptr); |
173 } | 221 switch (start_incremental_decode_result) { |
174 } | 222 case SkCodec::kSuccess: |
175 | 223 break; |
176 // Tell the frame to copy the row data if need be. | 224 case SkCodec::kIncompleteInput: |
177 if (repeat_count > 1) | 225 return; |
178 buffer.CopyRowNTimes(x_begin, x_end, y_begin, y_end); | 226 default: |
179 | 227 SetFailed(); |
180 buffer.SetPixelsChanged(true); | 228 return; |
181 return true; | 229 } |
182 } | 230 frame.SetStatus(ImageFrame::kFramePartial); |
183 | 231 } |
184 bool GIFImageDecoder::ParseCompleted() const { | 232 int rows_decoded = 0; |
185 return reader_ && reader_->ParseCompleted(); | 233 SkCodec::Result incremental_decode_result = |
186 } | 234 codec_->incrementalDecode(&rows_decoded); |
187 | 235 switch (incremental_decode_result) { |
188 bool GIFImageDecoder::FrameComplete(size_t frame_index) { | 236 case SkCodec::kSuccess: |
189 // Initialize the frame if necessary. Some GIFs insert do-nothing frames, | 237 frame.SetPixelsChanged(true); |
190 // in which case we never reach HaveDecodedRow() before getting here. | 238 frame.SetStatus(ImageFrame::kFrameComplete); |
191 if (!InitFrameBuffer(frame_index)) | 239 PostDecodeProcessing(index); |
192 return SetFailed(); | 240 break; |
193 | 241 case SkCodec::kIncompleteInput: |
194 if (!current_buffer_saw_alpha_) | 242 if (FrameIsCompleteAtIndex(index) || IsAllDataReceived()) { |
195 CorrectAlphaWhenFrameBufferSawNoAlpha(frame_index); | 243 SetFailed(); |
scroggo_chromium
2017/06/05 19:00:53
I think we may want to do this *after* zeroing the
cblume
2017/06/06 04:04:35
Done.
| |
196 | 244 return; |
197 frame_buffer_cache_[frame_index].SetStatus(ImageFrame::kFrameComplete); | 245 } |
198 | 246 { |
199 return true; | 247 IntRect remaining_rect = frame.OriginalFrameRect(); |
scroggo_chromium
2017/06/05 19:00:53
It seems weird to start with OriginalFrameRect. If
cblume
2017/06/06 04:04:35
Done.
| |
200 } | 248 remaining_rect.SetY(rows_decoded); |
201 | 249 remaining_rect.SetHeight(remaining_rect.Height() - rows_decoded); |
202 void GIFImageDecoder::ClearFrameBuffer(size_t frame_index) { | 250 frame.ZeroFillFrameRect(remaining_rect); |
scroggo_chromium
2017/06/05 19:00:53
We only need to do this the first time we get an i
cblume
2017/06/06 04:04:36
Done.
| |
203 if (reader_ && frame_buffer_cache_[frame_index].GetStatus() == | 251 // ZeroFillFrameRect() resets the alpha to true. |
scroggo_chromium
2017/06/05 19:00:53
I think you had a patch that changed this (but may
cblume
2017/06/06 04:04:36
Right. The CL is here:
https://codereview.chromium
| |
204 ImageFrame::kFramePartial) { | 252 // We want to preserve the alpha. So we need to set it back. |
205 // Reset the state of the partial frame in the reader so that the frame | 253 frame.SetHasAlpha(!SkAlphaTypeIsOpaque(frame_infos_[index].fAlphaType)); |
scroggo_chromium
2017/06/05 19:00:53
Maybe it would be clearer that we're setting it ba
cblume
2017/06/06 04:04:35
Done.
I went with the first version which I think
| |
206 // can be decoded again when requested. | 254 } |
207 reader_->ClearDecodeState(frame_index); | 255 |
208 } | 256 frame.SetPixelsChanged(true); |
209 ImageDecoder::ClearFrameBuffer(frame_index); | 257 break; |
210 } | 258 default: |
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(); | 259 SetFailed(); |
244 return; | 260 return; |
245 } | 261 } |
246 | 262 } |
247 // If this returns false, we need more data to continue decoding. | 263 |
248 if (!PostDecodeProcessing(*i)) | 264 bool GIFImageDecoder::CanReusePreviousFrameBuffer(size_t index) const { |
249 break; | 265 DCHECK(index < frame_buffer_cache_.size()); |
250 } | 266 |
251 | 267 // If the current frame and the next frame depend on the same frame, we cannot |
252 // It is also a fatal error if all data is received and we have decoded all | 268 // reuse the old frame. We must preserve it for the next frame. |
253 // frames available but the file is truncated. | 269 // |
254 if (index >= frame_buffer_cache_.size() - 1 && IsAllDataReceived() && | 270 // However, if the current and next frame depend on different frames then we |
255 reader_ && !reader_->ParseCompleted()) | 271 // know the current frame is the last one to use the frame it depends on. That |
256 SetFailed(); | 272 // means the current frame can reuse the previous frame buffer. |
257 } | 273 // |
258 | 274 // If we do not have information about the next frame yet, we cannot assume it |
259 void GIFImageDecoder::Parse(GIFParseQuery query) { | 275 // is safe to reuse the previous frame buffer. |
260 if (Failed()) | 276 |
261 return; | 277 if (index + 1 >= frame_buffer_cache_.size()) |
262 | 278 return false; |
263 if (!reader_) { | 279 |
264 reader_ = WTF::MakeUnique<GIFImageReader>(this); | 280 const ImageFrame& frame = frame_buffer_cache_[index]; |
265 reader_->SetData(data_); | 281 size_t required_frame_index = frame.RequiredPreviousFrameIndex(); |
266 } | 282 |
267 | 283 const ImageFrame& next_frame = frame_buffer_cache_[index + 1]; |
268 if (!reader_->Parse(query)) | 284 size_t next_required_frame_index = next_frame.RequiredPreviousFrameIndex(); |
269 SetFailed(); | 285 |
270 } | 286 return required_frame_index != next_required_frame_index; |
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 } | 287 } |
281 | 288 |
282 } // namespace blink | 289 } // namespace blink |
OLD | NEW |