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_(kCAnimationLoopOnce) {} | 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, false); | |
55 return; | |
56 } | |
57 | |
58 if (!segment_stream_) | |
59 segment_stream_ = new SegmentStream(); | |
60 | |
61 segment_stream_->SetReader(data, IsAllDataReceived()); | |
62 | |
63 // If we don't have a SkCodec yet, create one from the stream | |
64 if (!codec_) { | |
65 codec_.reset(SkCodec::NewFromStream(segment_stream_, nullptr)); | |
66 if (!codec_) { | |
67 // |segment_stream_|'s ownership is passed into NewFromStream. | |
68 // It is deleted if NewFromStream fails. | |
69 // If NewFromStream fails, we set |segment_stream_| to null so | |
70 // we aren't pointing to reclaimed memory. | |
71 segment_stream_ = nullptr; | |
72 return; | |
73 } | |
74 | |
75 // SkCodec::NewFromStream will read enough of the image to get the image | |
76 // size. | |
77 SkImageInfo image_info = codec_->getInfo(); | |
78 SetSize(image_info.width(), image_info.height()); | |
79 } | |
46 } | 80 } |
47 | 81 |
48 int GIFImageDecoder::RepetitionCount() const { | 82 int GIFImageDecoder::RepetitionCount() const { |
83 if (!codec_) | |
84 return kCAnimationLoopOnce; | |
85 | |
49 // This value can arrive at any point in the image data stream. Most GIFs | 86 // 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 | 87 // 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 | 88 // 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 | 89 // packets sent back by the webserver) not always. |
53 // seen a loop count yet, it will return cLoopCountNotSeen, in which case we | |
54 // should default to looping once (the initial value for | |
55 // |m_repetitionCount|). | |
56 // | 90 // |
57 // There are some additional wrinkles here. First, ImageSource::clear() | 91 // 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_ | 92 // seen yet. |
59 // authoritative on future calls if the recreated reader hasn't seen the | 93 |
60 // loop count. We don't need to special-case this because in this case the | 94 if (IsAllDataReceived() && frame_infos_.size() == 1) |
61 // new reader will once again return cLoopCountNotSeen, and we won't | 95 return kCAnimationNone; |
62 // overwrite the cached correct value. | 96 if (Failed()) |
63 // | 97 return kCAnimationLoopOnce; |
64 // Second, a GIF might never set a loop count at all, in which case we | 98 |
65 // should continue to treat it as a "loop once" animation. We don't need | 99 int repetition_count = codec_->getRepetitionCount(); |
66 // special code here either, because in this case we'll never change | 100 switch (repetition_count) { |
67 // |m_repetitionCount| from its default value. | 101 case 0: |
68 // | 102 return kCAnimationLoopOnce; |
69 // Third, we use the same GIFImageReader for counting frames and we might | 103 case SkCodec::kRepetitionCountInfinite: |
70 // see the loop count and then encounter a decoding error which happens | 104 return kCAnimationLoopInfinite; |
71 // later in the stream. It is also possible that no frames are in the | 105 default: |
72 // stream. In these cases we should just loop once. | 106 return repetition_count; |
73 if (IsAllDataReceived() && ParseCompleted() && reader_->imagesCount() == 1) | 107 } |
74 repetition_count_ = kCAnimationNone; | |
75 else if (Failed() || (reader_ && (!reader_->imagesCount()))) | |
76 repetition_count_ = kCAnimationLoopOnce; | |
77 else if (reader_ && reader_->loopCount() != cLoopCountNotSeen) | |
78 repetition_count_ = reader_->loopCount(); | |
79 return repetition_count_; | |
80 } | 108 } |
81 | 109 |
82 bool GIFImageDecoder::FrameIsCompleteAtIndex(size_t index) const { | 110 bool GIFImageDecoder::FrameIsCompleteAtIndex(size_t index) const { |
83 return reader_ && (index < reader_->imagesCount()) && | 111 if (!codec_) |
84 reader_->frameContext(index)->isComplete(); | 112 return false; |
113 | |
114 if (frame_infos_.size() <= index) | |
115 return false; | |
116 | |
117 return frame_infos_[index].fFullyReceived; | |
85 } | 118 } |
86 | 119 |
87 float GIFImageDecoder::FrameDurationAtIndex(size_t index) const { | 120 float GIFImageDecoder::FrameDurationAtIndex(size_t index) const { |
88 return (reader_ && (index < reader_->imagesCount()) && | 121 if (index < frame_buffer_cache_.size()) |
89 reader_->frameContext(index)->isHeaderDefined()) | 122 return frame_buffer_cache_[index].Duration(); |
90 ? reader_->frameContext(index)->delayTime() | 123 return 0; |
91 : 0; | |
92 } | 124 } |
93 | 125 |
94 bool GIFImageDecoder::SetFailed() { | 126 bool GIFImageDecoder::SetFailed() { |
95 reader_.reset(); | 127 segment_stream_ = nullptr; |
128 codec_ = nullptr; | |
scroggo_chromium
2017/04/21 20:09:32
Presumably codec_ is never null in this situation.
cblume
2017/04/21 23:47:38
I see what you're saying.
Since this only happens
scroggo_chromium
2017/04/24 15:06:54
DCHECK(codec_);
should be a good defense.
cblume
2017/04/24 18:22:33
Done.
| |
96 return ImageDecoder::SetFailed(); | 129 return ImageDecoder::SetFailed(); |
97 } | 130 } |
98 | 131 |
99 bool GIFImageDecoder::HaveDecodedRow(size_t frame_index, | 132 size_t GIFImageDecoder::DecodeFrameCount() { |
100 GIFRow::const_iterator row_begin, | 133 if (!codec_) |
101 size_t width, | 134 return 0; |
102 size_t row_number, | 135 |
103 unsigned repeat_count, | 136 if (!Failed() && (segment_stream_ && !segment_stream_->IsCleared())) |
104 bool write_transparent_pixels) { | 137 frame_infos_ = codec_->getFrameInfo(); |
105 const GIFFrameContext* frame_context = reader_->frameContext(frame_index); | 138 return frame_infos_.size(); |
106 // The pixel data and coordinates supplied to us are relative to the frame's | 139 } |
107 // origin within the entire image size, i.e. | 140 |
108 // (frameContext->xOffset, frameContext->yOffset). There is no guarantee | 141 void GIFImageDecoder::InitializeNewFrame(size_t index) { |
109 // that width == (size().width() - frameContext->xOffset), so | 142 DCHECK(codec_); |
110 // we must ensure we don't run off the end of either the source data or the | 143 |
111 // row's X-coordinates. | 144 ImageFrame& frame = frame_buffer_cache_[index]; |
112 const int x_begin = frame_context->xOffset(); | 145 // SkCodec does not inform us if only a portion of the image was updated |
113 const int y_begin = frame_context->yOffset() + row_number; | 146 // 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), | 147 // the frame rect, we set the frame rect to be the image's full size. |
115 Size().Width()); | 148 IntSize full_image_size = Size(); |
116 const int y_end = std::min( | 149 frame.SetOriginalFrameRect(IntRect(IntPoint(), full_image_size)); |
117 static_cast<int>(frame_context->yOffset() + row_number + repeat_count), | 150 frame.SetDuration(frame_infos_[index].fDuration); |
118 Size().Height()); | 151 frame.SetHasAlpha(!SkAlphaTypeIsOpaque(frame_infos_[index].fAlphaType)); |
119 if (!width || (x_begin < 0) || (y_begin < 0) || (x_end <= x_begin) || | 152 size_t required_previous_frame_index; |
120 (y_end <= y_begin)) | 153 if (frame_infos_[index].fRequiredFrame == SkCodec::kNone) { |
121 return true; | 154 required_previous_frame_index = WTF::kNotFound; |
122 | 155 } else { |
123 const GIFColorMap::Table& color_table = | 156 required_previous_frame_index = |
124 frame_context->localColorMap().isDefined() | 157 static_cast<size_t>(frame_infos_[index].fRequiredFrame); |
125 ? frame_context->localColorMap().getTable() | 158 } |
126 : reader_->globalColorMap().getTable(); | 159 frame.SetRequiredPreviousFrameIndex(required_previous_frame_index); |
127 | 160 // The disposal method is not required any more, but is left in place |
128 if (color_table.IsEmpty()) | 161 // for the other image decoders that do not yet rely on SkCodec. |
129 return true; | 162 // For now, fill it with DisposeKeep. |
130 | 163 frame.SetDisposalMethod(ImageFrame::kDisposeKeep); |
131 GIFColorMap::Table::const_iterator color_table_iter = color_table.begin(); | 164 } |
132 | 165 |
133 // Initialize the frame if necessary. | 166 void GIFImageDecoder::Decode(size_t index) { |
134 ImageFrame& buffer = frame_buffer_cache_[frame_index]; | 167 if (Failed()) |
135 if (!InitFrameBuffer(frame_index)) | 168 return; |
136 return false; | 169 |
137 | 170 if (!codec_) |
138 const size_t transparent_pixel = frame_context->transparentPixel(); | 171 return; |
139 GIFRow::const_iterator row_end = row_begin + (x_end - x_begin); | 172 |
140 ImageFrame::PixelData* current_address = buffer.GetAddr(x_begin, y_begin); | 173 if (segment_stream_ && segment_stream_->IsCleared()) |
141 | 174 return; |
142 // We may or may not need to write transparent pixels to the buffer. | 175 |
143 // If we're compositing against a previous image, it's wrong, and if | 176 if (frame_buffer_cache_.size() <= index) { |
144 // we're writing atop a cleared, fully transparent buffer, it's | 177 // It is a fatal error if all data is received and we have decoded all |
145 // unnecessary; but if we're decoding an interlaced gif and | 178 // frames available but the file is truncated. |
146 // displaying it "Haeberli"-style, we must write these for passes | 179 if (IsAllDataReceived()) |
147 // beyond the first, or the initial passes will "show through" the | 180 SetFailed(); |
148 // later ones. | 181 |
149 // | 182 return; |
150 // The loops below are almost identical. One writes a transparent pixel | 183 } |
151 // and one doesn't based on the value of |writeTransparentPixels|. | 184 |
152 // The condition check is taken out of the loop to enhance performance. | 185 UpdateAggressivePurging(index); |
153 // This optimization reduces decoding time by about 15% for a 3MB image. | 186 SkImageInfo image_info = codec_->getInfo().makeColorType(kN32_SkColorType); |
154 if (write_transparent_pixels) { | 187 |
155 for (; row_begin != row_end; ++row_begin, ++current_address) { | 188 SkCodec::Options options; |
156 const size_t source_value = *row_begin; | 189 options.fFrameIndex = index; |
157 if ((source_value != transparent_pixel) && | 190 options.fHasPriorFrame = false; |
158 (source_value < color_table.size())) { | 191 options.fZeroInitialized = SkCodec::kNo_ZeroInitialized; |
159 *current_address = color_table_iter[source_value]; | 192 |
160 } else { | 193 ImageFrame& frame = frame_buffer_cache_[index]; |
161 *current_address = 0; | 194 if (frame.GetStatus() == ImageFrame::kFrameEmpty) { |
162 current_buffer_saw_alpha_ = true; | 195 size_t required_previous_frame_index = frame.RequiredPreviousFrameIndex(); |
196 if (required_previous_frame_index == WTF::kNotFound) { | |
197 frame.AllocatePixelData(Size().Width(), Size().Height(), | |
198 ColorSpaceForSkImages()); | |
199 frame.SetStatus(ImageFrame::kFrameAllocated); | |
scroggo_chromium
2017/04/21 20:09:32
Should this be set inside AllocatePixelData?
cblume
2017/04/21 23:47:38
Mmmm maybe. I also need it in the else branch here
scroggo_chromium
2017/04/24 15:06:54
Another option - change the status to Allocated in
cblume
2017/04/24 18:22:33
Done.
| |
200 frame.SetHasAlpha(!SkAlphaTypeIsOpaque(frame_infos_[index].fAlphaType)); | |
201 } else { | |
202 ImageFrame& required_previous_frame = | |
203 frame_buffer_cache_[required_previous_frame_index]; | |
204 | |
205 if (required_previous_frame.GetStatus() != ImageFrame::kFrameComplete) | |
206 Decode(required_previous_frame_index); | |
207 | |
208 // We try to reuse |required_previous_frame| as starting state to avoid | |
209 // copying. If CanReusePreviousFrameBuffer returns false, we must copy | |
210 // the data since |required_previous_frame| is necessary to decode this | |
211 // or later frames. In that case copy the data instead. | |
212 if ((!CanReusePreviousFrameBuffer(index) || | |
213 !frame.TakeBitmapDataIfWritable(&required_previous_frame)) && | |
214 !frame.CopyBitmapData(required_previous_frame)) { | |
215 SetFailed(); | |
216 return; | |
163 } | 217 } |
218 options.fHasPriorFrame = true; | |
scroggo_chromium
2017/04/21 20:09:32
After this, what should the FrameStatus be? Lookin
cblume
2017/04/21 23:47:38
Hahaha. Good eye. That is exactly the bug I fixed
| |
164 } | 219 } |
165 } else { | 220 } |
166 for (; row_begin != row_end; ++row_begin, ++current_address) { | 221 |
167 const size_t source_value = *row_begin; | 222 if (frame.GetStatus() == ImageFrame::kFrameAllocated) { |
168 if ((source_value != transparent_pixel) && | 223 SkCodec::Result start_incremental_decode_result = |
169 (source_value < color_table.size())) | 224 codec_->startIncrementalDecode(image_info, frame.Bitmap().getPixels(), |
170 *current_address = color_table_iter[source_value]; | 225 frame.Bitmap().rowBytes(), &options, |
171 else | 226 nullptr, nullptr); |
172 current_buffer_saw_alpha_ = true; | 227 switch (start_incremental_decode_result) { |
228 case SkCodec::kSuccess: | |
229 break; | |
230 case SkCodec::kIncompleteInput: | |
231 return; | |
232 default: | |
233 SetFailed(); | |
234 return; | |
173 } | 235 } |
174 } | 236 frame.SetStatus(ImageFrame::kFramePartial); |
175 | 237 } |
176 // Tell the frame to copy the row data if need be. | 238 int rows_decoded = 0; |
177 if (repeat_count > 1) | 239 SkCodec::Result incremental_decode_result = |
178 buffer.CopyRowNTimes(x_begin, x_end, y_begin, y_end); | 240 codec_->incrementalDecode(&rows_decoded); |
179 | 241 switch (incremental_decode_result) { |
180 buffer.SetPixelsChanged(true); | 242 case SkCodec::kSuccess: |
181 return true; | 243 frame.SetHasAlpha(!SkAlphaTypeIsOpaque(frame_infos_[index].fAlphaType)); |
182 } | 244 frame.SetPixelsChanged(true); |
183 | 245 frame.SetStatus(ImageFrame::kFrameComplete); |
184 bool GIFImageDecoder::ParseCompleted() const { | 246 PostDecodeProcessing(index); |
185 return reader_ && reader_->parseCompleted(); | 247 break; |
186 } | 248 case SkCodec::kIncompleteInput: |
187 | 249 if (FrameIsCompleteAtIndex(index) || IsAllDataReceived()) { |
188 bool GIFImageDecoder::FrameComplete(size_t frame_index) { | 250 SetFailed(); |
189 // Initialize the frame if necessary. Some GIFs insert do-nothing frames, | 251 return; |
190 // in which case we never reach haveDecodedRow() before getting here. | 252 } |
191 if (!InitFrameBuffer(frame_index)) | 253 { |
192 return SetFailed(); | 254 IntRect remaining_rect = frame.OriginalFrameRect(); |
193 | 255 remaining_rect.SetY(rows_decoded); |
194 if (!current_buffer_saw_alpha_) | 256 remaining_rect.SetHeight(remaining_rect.Height() - rows_decoded); |
195 CorrectAlphaWhenFrameBufferSawNoAlpha(frame_index); | 257 frame.ZeroFillFrameRect(remaining_rect); |
196 | 258 frame.SetHasAlpha(true); |
197 frame_buffer_cache_[frame_index].SetStatus(ImageFrame::kFrameComplete); | 259 } |
198 | 260 |
199 return true; | 261 frame.SetPixelsChanged(true); |
200 } | 262 break; |
201 | 263 default: |
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, |m_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(); | 264 SetFailed(); |
244 return; | 265 return; |
245 } | 266 } |
246 | 267 } |
247 // If this returns false, we need more data to continue decoding. | 268 |
248 if (!PostDecodeProcessing(*i)) | 269 bool GIFImageDecoder::CanReusePreviousFrameBuffer(size_t index) const { |
249 break; | 270 DCHECK(index < frame_buffer_cache_.size()); |
250 } | 271 |
251 | 272 // 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 | 273 // reuse the old frame. We must preserve it for the next frame. |
253 // frames available but the file is truncated. | 274 // |
254 if (index >= frame_buffer_cache_.size() - 1 && IsAllDataReceived() && | 275 // However, if the current and next frame depend on different frames then we |
255 reader_ && !reader_->parseCompleted()) | 276 // know the current frame is the last one to use the frame it depends on. That |
256 SetFailed(); | 277 // means the current frame can reuse the previous frame buffer. |
257 } | 278 // |
258 | 279 // If we do not have information about the next frame yet, we cannot assume it |
259 void GIFImageDecoder::Parse(GIFParseQuery query) { | 280 // is safe to reuse the previous frame buffer. |
260 if (Failed()) | 281 |
261 return; | 282 if (index + 1 >= frame_buffer_cache_.size()) |
262 | 283 return false; |
263 if (!reader_) { | 284 |
264 reader_ = WTF::MakeUnique<GIFImageReader>(this); | 285 const ImageFrame& frame = frame_buffer_cache_[index]; |
265 reader_->setData(data_); | 286 size_t required_frame_index = frame.RequiredPreviousFrameIndex(); |
266 } | 287 |
267 | 288 const ImageFrame& next_frame = frame_buffer_cache_[index + 1]; |
268 if (!reader_->parse(query)) | 289 size_t next_required_frame_index = next_frame.RequiredPreviousFrameIndex(); |
269 SetFailed(); | 290 |
270 } | 291 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 } | 292 } |
281 | 293 |
282 } // namespace blink | 294 } // namespace blink |
OLD | NEW |