Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(53)

Side by Side Diff: third_party/WebKit/Source/platform/image-decoders/gif/GIFImageDecoder.cpp

Issue 2565323003: Move gif image decoder to SkCodec (Closed)
Patch Set: Support SkCodec::NewFromStream finding an error in the input Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
42 GIFImageDecoder::~GIFImageDecoder() {
43 if (!codec_) {
44 // if we did not create |codec_| and thus did not pass ownership to it
45 if (segment_stream_)
46 delete segment_stream_;
47 }
48 }
42 49
43 void GIFImageDecoder::OnSetData(SegmentReader* data) { 50 void GIFImageDecoder::OnSetData(SegmentReader* data) {
44 if (reader_) 51 if (!data) {
45 reader_->SetData(data); 52 if (segment_stream_)
53 segment_stream_->SetReader(nullptr);
54 return;
55 }
56
57 if (!segment_stream_)
58 segment_stream_ = new SegmentStream();
59
60 segment_stream_->SetReader(PassRefPtr<SegmentReader>(data));
61
62 if (!codec_) {
63 SkCodec::Result stream_creation_result;
64 codec_.reset(SkCodec::NewFromStream(segment_stream_,
vmpstr 2017/07/17 21:28:40 It seems kind of awkward that a codec_ takes owner
cblume 2017/07/18 10:23:49 I agree. One option would be a shared ownership.
scroggo_chromium 2017/07/18 17:52:42 SkStream was originally meant to be something you
65 &stream_creation_result, nullptr));
66 switch (stream_creation_result) {
67 case SkCodec::kSuccess: {
68 // SkCodec::NewFromStream will read enough of the image to get the image
69 // size.
70 SkImageInfo image_info = codec_->getInfo();
71 SetSize(image_info.width(), image_info.height());
72 return;
73 }
74 case SkCodec::kIncompleteInput:
75 // |segment_stream_|'s ownership is passed into NewFromStream.
76 // It is deleted if NewFromStream fails.
77 // If NewFromStream fails, we set |segment_stream_| to null so
78 // we aren't pointing to reclaimed memory.
79 segment_stream_ = nullptr;
80 return;
81 default:
82 segment_stream_ = nullptr;
vmpstr 2017/07/17 21:28:40 nit: SetFailed will do this?
cblume 2017/07/18 10:23:49 Done.
83 SetFailed();
vmpstr 2017/07/17 21:28:40 SetFailed DCHECKs that codec_ exists, but it's not
cblume 2017/07/18 10:23:49 You are right. The call to SetFailed() still needs
84 return;
85 }
86 }
46 } 87 }
47 88
48 int GIFImageDecoder::RepetitionCount() const { 89 int GIFImageDecoder::RepetitionCount() const {
90 if (!codec_)
91 return kAnimationLoopOnce;
92
93 DCHECK(!Failed());
94
49 // This value can arrive at any point in the image data stream. Most GIFs 95 // 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 96 // 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 97 // 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 98 // 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 // 99 //
57 // There are some additional wrinkles here. First, ImageSource::Clear() 100 // 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_ 101 // seen yet.
59 // authoritative on future calls if the recreated reader hasn't seen the 102 int repetition_count = codec_->getRepetitionCount();
60 // loop count. We don't need to special-case this because in this case the 103
61 // new reader will once again return kCLoopCountNotSeen, and we won't 104 switch (repetition_count) {
62 // overwrite the cached correct value. 105 case 0: {
vmpstr 2017/07/17 21:28:40 Please leave a comment about the fact that SkCodec
cblume 2017/07/18 10:23:49 Done.
106 size_t frame_count = codec_->getFrameCount();
107 if (IsAllDataReceived() && frame_count == 1)
108 return kAnimationNone;
109
110 return kAnimationLoopOnce;
111 }
112 case SkCodec::kRepetitionCountInfinite:
113 return kAnimationLoopInfinite;
114 default:
115 return repetition_count;
116 }
117 }
118
119 bool GIFImageDecoder::FrameIsCompleteAtIndex(size_t index) const {
120 if (!codec_)
121 return false;
122
123 if (static_cast<size_t>(codec_->getFrameCount()) <= index)
124 return false;
125
126 SkCodec::FrameInfo frame_info;
127 codec_->getFrameInfo(index, &frame_info);
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 DCHECK(codec_);
139
140 segment_stream_ = nullptr;
141 codec_ = nullptr;
142 return ImageDecoder::SetFailed();
143 }
144
145 size_t GIFImageDecoder::ClearCacheExceptFrame(size_t index) {
scroggo_chromium 2017/07/17 20:07:56 First off, I'll acknowledge that this is intended
cblume 2017/07/18 10:23:49 I may agree. I kept it as similar as possible to t
scroggo_chromium 2017/07/18 17:52:42 Makes sense.
cblume 2017/07/19 23:27:04 Yeah, I think this will just disappear once the ca
146 // SkCodec marks a required previous frame as the first frame in
147 // [required_ptrevious_frame, index). Other Blink image decoders instead would
scroggo_chromium 2017/07/17 20:07:56 required_previous_frame (no 't')
cblume 2017/07/18 10:23:49 Done.
148 // have marked the last index in that range.
scroggo_chromium 2017/07/17 20:07:56 This comment is confusing: - It doesn't really des
cblume 2017/07/18 10:23:49 Done.
63 // 149 //
64 // Second, a GIF might never set a loop count at all, in which case we 150 // When we clear the cache, we preserve a previous frame if the frame at
scroggo_chromium 2017/07/17 20:07:56 This tells what, but not why. I think it can be el
cblume 2017/07/18 10:23:49 Done.
65 // should continue to treat it as a "loop once" animation. We don't need 151 // |index| is kDisposePrevious.
66 // special code here either, because in this case we'll never change
67 // |repetition_count_| from its default value.
68 // 152 //
69 // Third, we use the same GIFImageReader for counting frames and we might 153 // This means SkCodec-based decoders must override ClearCacheExceptFrame() to
scroggo_chromium 2017/07/17 20:07:56 This line seems redundant - this is inside the ove
cblume 2017/07/18 10:23:49 Done. I wanted to illustrate that it was really f
70 // see the loop count and then encounter a decoding error which happens 154 // not keep the marked required previous frame in the case of
71 // later in the stream. It is also possible that no frames are in the 155 // kDisposePrevious. They should instead keep the last frame in the range,
scroggo_chromium 2017/07/17 20:07:56 Don't necessarily need the last frame in the range
cblume 2017/07/18 10:23:49 I removed this comment and went with what you put
72 // stream. In these cases we should just loop once. 156 // like other Blink decoders do.
73 if (IsAllDataReceived() && ParseCompleted() && reader_->ImagesCount() == 1) 157 if (frame_buffer_cache_.size() <= 1)
74 repetition_count_ = kAnimationNone; 158 return 0;
75 else if (Failed() || (reader_ && (!reader_->ImagesCount()))) 159
76 repetition_count_ = kAnimationLoopOnce; 160 size_t index2 = kNotFound;
77 else if (reader_ && reader_->LoopCount() != kCLoopCountNotSeen) 161 if (index < frame_buffer_cache_.size()) {
78 repetition_count_ = reader_->LoopCount(); 162 const ImageFrame& frame = frame_buffer_cache_[index];
79 return repetition_count_; 163 if (!FrameStatusSufficientForSuccessors(index) ||
scroggo_chromium 2017/07/17 20:07:56 Since this is only called here (i.e. not by the ba
cblume 2017/07/18 10:23:49 The base class version needed to be updated. It on
80 } 164 frame.GetDisposalMethod() == ImageFrame::kDisposeOverwritePrevious) {
81 165 // We need to preserve a previous frame
82 bool GIFImageDecoder::FrameIsCompleteAtIndex(size_t index) const { 166 index2 = GetPreviousReferenceFrame(index);
83 return reader_ && (index < reader_->ImagesCount()) && 167 }
84 reader_->FrameContext(index)->IsComplete(); 168 }
85 } 169
86 170 while (index2 < frame_buffer_cache_.size() &&
87 float GIFImageDecoder::FrameDurationAtIndex(size_t index) const { 171 !FrameStatusSufficientForSuccessors(index2)) {
scroggo_chromium 2017/07/17 20:07:56 This call makes sense in theory, although I suspec
cblume 2017/07/18 10:23:49 I think you are right. I think the call to GetPre
scroggo_chromium 2017/07/18 17:52:42 Now that I look at it more closely, if nothing in
cblume 2017/07/19 23:27:04 Right. I actually removed the second loop. The sec
scroggo_chromium 2017/07/20 13:59:52 Not quite - GetPreviousReferenceFrame() goes back
88 return (reader_ && (index < reader_->ImagesCount()) && 172 index2 = GetPreviousReferenceFrame(index2);
89 reader_->FrameContext(index)->IsHeaderDefined()) 173 }
90 ? reader_->FrameContext(index)->DelayTime() 174
91 : 0; 175 return ClearCacheExceptTwoFrames(index, index2);
92 } 176 }
93 177
94 bool GIFImageDecoder::SetFailed() { 178 size_t GIFImageDecoder::DecodeFrameCount() {
95 reader_.reset(); 179 if (!codec_)
96 return ImageDecoder::SetFailed(); 180 return 0;
97 } 181
98 182 if (Failed() || segment_stream_->IsCleared())
99 bool GIFImageDecoder::HaveDecodedRow(size_t frame_index, 183 return 0;
100 GIFRow::const_iterator row_begin, 184
101 size_t width, 185 return codec_->getFrameCount();
102 size_t row_number, 186 }
103 unsigned repeat_count, 187
104 bool write_transparent_pixels) { 188 void GIFImageDecoder::InitializeNewFrame(size_t index) {
105 const GIFFrameContext* frame_context = reader_->FrameContext(frame_index); 189 DCHECK(codec_);
106 // The pixel data and coordinates supplied to us are relative to the frame's 190
107 // origin within the entire image size, i.e. 191 ImageFrame& frame = frame_buffer_cache_[index];
108 // (frameC_context->xOffset, frame_context->yOffset). There is no guarantee 192 // SkCodec does not inform us if only a portion of the image was updated
109 // that width == (size().width() - frame_context->xOffset), so 193 // in the current frame. Because of this, rather than correctly filling in
110 // we must ensure we don't run off the end of either the source data or the 194 // the frame rect, we set the frame rect to be the image's full size.
111 // row's X-coordinates. 195 // The original frame rect is not used, anyway.
112 const int x_begin = frame_context->XOffset(); 196 IntSize full_image_size = Size();
113 const int y_begin = frame_context->YOffset() + row_number; 197 frame.SetOriginalFrameRect(IntRect(IntPoint(), full_image_size));
114 const int x_end = std::min(static_cast<int>(frame_context->XOffset() + width), 198
115 Size().Width()); 199 SkCodec::FrameInfo frame_info;
116 const int y_end = std::min( 200 codec_->getFrameInfo(index, &frame_info);
117 static_cast<int>(frame_context->YOffset() + row_number + repeat_count), 201 frame.SetDuration(frame_info.fDuration);
118 Size().Height()); 202 frame.SetHasAlpha(!SkAlphaTypeIsOpaque(frame_info.fAlphaType));
119 if (!width || (x_begin < 0) || (y_begin < 0) || (x_end <= x_begin) || 203 size_t required_previous_frame_index;
120 (y_end <= y_begin)) 204 if (frame_info.fRequiredFrame == SkCodec::kNone) {
121 return true; 205 required_previous_frame_index = WTF::kNotFound;
122 206 } else {
123 const GIFColorMap::Table& color_table = 207 required_previous_frame_index =
124 frame_context->LocalColorMap().IsDefined() 208 static_cast<size_t>(frame_info.fRequiredFrame);
125 ? frame_context->LocalColorMap().GetTable() 209 }
126 : reader_->GlobalColorMap().GetTable(); 210 frame.SetRequiredPreviousFrameIndex(required_previous_frame_index);
127 211
128 if (color_table.IsEmpty()) 212 ImageFrame::DisposalMethod disposal_method = ImageFrame::kDisposeNotSpecified;
129 return true; 213 switch (frame_info.fDisposalMethod) {
130 214 case SkCodecAnimation::DisposalMethod::kKeep:
131 GIFColorMap::Table::const_iterator color_table_iter = color_table.begin(); 215 disposal_method = ImageFrame::kDisposeKeep;
132 216 break;
133 // Initialize the frame if necessary. 217 case SkCodecAnimation::DisposalMethod::kRestoreBGColor:
134 ImageFrame& buffer = frame_buffer_cache_[frame_index]; 218 disposal_method = ImageFrame::kDisposeOverwriteBgcolor;
135 if (!InitFrameBuffer(frame_index)) 219 break;
136 return false; 220 case SkCodecAnimation::DisposalMethod::kRestorePrevious:
137 221 disposal_method = ImageFrame::kDisposeOverwritePrevious;
138 const size_t transparent_pixel = frame_context->TransparentPixel(); 222 break;
139 GIFRow::const_iterator row_end = row_begin + (x_end - x_begin); 223 }
140 ImageFrame::PixelData* current_address = buffer.GetAddr(x_begin, y_begin); 224 frame.SetDisposalMethod(disposal_method);
141 225 frame.SetStatus(ImageFrame::kFrameEmpty);
scroggo_chromium 2017/07/17 20:07:56 Why is this line necessary? Doesn't frame start of
cblume 2017/07/18 10:23:49 Done.
142 // We may or may not need to write transparent pixels to the buffer. 226 }
143 // If we're compositing against a previous image, it's wrong, and if 227
144 // we're writing atop a cleared, fully transparent buffer, it's 228 void GIFImageDecoder::Decode(size_t index) {
145 // unnecessary; but if we're decoding an interlaced gif and 229 if (!codec_)
146 // displaying it "Haeberli"-style, we must write these for passes 230 return;
147 // beyond the first, or the initial passes will "show through" the 231
148 // later ones. 232 DCHECK(!Failed());
149 // 233
150 // The loops below are almost identical. One writes a transparent pixel 234 DCHECK_LT(index, frame_buffer_cache_.size());
151 // and one doesn't based on the value of |write_transparent_pixels|. 235
152 // The condition check is taken out of the loop to enhance performance. 236 if (segment_stream_->IsCleared())
153 // This optimization reduces decoding time by about 15% for a 3MB image. 237 return;
154 if (write_transparent_pixels) { 238
155 for (; row_begin != row_end; ++row_begin, ++current_address) { 239 UpdateAggressivePurging(index);
156 const size_t source_value = *row_begin; 240 SkImageInfo image_info = codec_->getInfo()
157 if ((source_value != transparent_pixel) && 241 .makeColorType(kN32_SkColorType)
158 (source_value < color_table.size())) { 242 .makeColorSpace(ColorSpaceForSkImages());
159 *current_address = color_table_iter[source_value]; 243
160 } else { 244 SkCodec::Options options;
161 *current_address = 0; 245 options.fFrameIndex = index;
162 current_buffer_saw_alpha_ = true; 246 options.fPriorFrame = SkCodec::kNone;
247 options.fZeroInitialized = SkCodec::kNo_ZeroInitialized;
248
249 ImageFrame& frame = frame_buffer_cache_[index];
250 if (frame.GetStatus() == ImageFrame::kFrameEmpty) {
251 size_t required_previous_frame_index = frame.RequiredPreviousFrameIndex();
252 if (required_previous_frame_index == WTF::kNotFound) {
253 frame.AllocatePixelData(Size().Width(), Size().Height(),
254 ColorSpaceForSkImages());
255 bool oldAlpha = frame.HasAlpha();
256 frame.ZeroFillPixelData();
257 options.fZeroInitialized = SkCodec::kYes_ZeroInitialized;
258 frame.SetHasAlpha(oldAlpha);
259 } else {
260 size_t previous_frame_index = GetPreviousReferenceFrame(index);
261 if (previous_frame_index == kNotFound)
262 previous_frame_index = required_previous_frame_index;
263
264 ImageFrame& previous_frame = frame_buffer_cache_[previous_frame_index];
265 if (previous_frame.GetStatus() != ImageFrame::kFrameComplete)
266 Decode(previous_frame_index);
267
268 // We try to reuse |previous_frame| as starting state to avoid copying.
269 // If CanReusePreviousFrameBuffer returns false, we must copy the data
270 // since |previous_frame| is necessary to decode this or later frames.
271 // In that case copy the data instead.
272 if ((!CanReusePreviousFrameBuffer(index) ||
273 !frame.TakeBitmapDataIfWritable(&previous_frame)) &&
274 !frame.CopyBitmapData(previous_frame)) {
275 SetFailed();
276 return;
163 } 277 }
164 } 278 options.fPriorFrame = previous_frame_index;
165 } else { 279 }
166 for (; row_begin != row_end; ++row_begin, ++current_address) { 280 }
167 const size_t source_value = *row_begin; 281
168 if ((source_value != transparent_pixel) && 282 if (frame.GetStatus() == ImageFrame::kFrameAllocated) {
169 (source_value < color_table.size())) 283 SkCodec::Result start_incremental_decode_result =
170 *current_address = color_table_iter[source_value]; 284 codec_->startIncrementalDecode(image_info, frame.Bitmap().getPixels(),
171 else 285 frame.Bitmap().rowBytes(), &options);
172 current_buffer_saw_alpha_ = true; 286 switch (start_incremental_decode_result) {
173 } 287 case SkCodec::kSuccess:
174 } 288 break;
175 289 case SkCodec::kIncompleteInput:
176 // Tell the frame to copy the row data if need be. 290 return;
177 if (repeat_count > 1) 291 default:
178 buffer.CopyRowNTimes(x_begin, x_end, y_begin, y_end); 292 SetFailed();
179 293 return;
180 buffer.SetPixelsChanged(true); 294 }
181 return true; 295 frame.SetStatus(ImageFrame::kFramePartial);
182 } 296 }
183 297 int rows_decoded = 0;
184 bool GIFImageDecoder::ParseCompleted() const { 298 SkCodec::Result incremental_decode_result =
185 return reader_ && reader_->ParseCompleted(); 299 codec_->incrementalDecode(&rows_decoded);
186 } 300 switch (incremental_decode_result) {
187 301 case SkCodec::kSuccess:
188 bool GIFImageDecoder::FrameComplete(size_t frame_index) { 302 frame.SetPixelsChanged(true);
189 // Initialize the frame if necessary. Some GIFs insert do-nothing frames, 303 frame.SetStatus(ImageFrame::kFrameComplete);
190 // in which case we never reach HaveDecodedRow() before getting here. 304 PostDecodeProcessing(index);
191 if (!InitFrameBuffer(frame_index)) 305 break;
192 return SetFailed(); 306 case SkCodec::kIncompleteInput:
193 307 frame.SetPixelsChanged(true);
194 if (!current_buffer_saw_alpha_) 308 if (FrameIsCompleteAtIndex(index) || IsAllDataReceived()) {
195 CorrectAlphaWhenFrameBufferSawNoAlpha(frame_index); 309 SetFailed();
196 310 return;
197 frame_buffer_cache_[frame_index].SetStatus(ImageFrame::kFrameComplete); 311 }
198 312
199 return true; 313 break;
200 } 314 default:
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(); 315 SetFailed();
244 return; 316 return;
245 } 317 }
246 318 }
247 // If this returns false, we need more data to continue decoding. 319
248 if (!PostDecodeProcessing(*i)) 320 bool GIFImageDecoder::CanReusePreviousFrameBuffer(size_t index) const {
249 break; 321 DCHECK(index < frame_buffer_cache_.size());
250 } 322 return frame_buffer_cache_[index].GetDisposalMethod() !=
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; 323 ImageFrame::kDisposeOverwritePrevious;
280 } 324 }
281 325
326 size_t GIFImageDecoder::GetPreviousReferenceFrame(size_t index) const {
327 // Any frame in the range [required_previous_frame_index, index) which
scroggo_chromium 2017/07/17 20:07:56 nit: required_previous_frame_index hasn't been use
cblume 2017/07/18 10:23:49 I moved the declaration of required_previous_Frame
328 // has a disposal method other than kRestorePrevious can be provided as
329 // the prior frame to SkCodec.
330 //
331 // This is because SkCodec sets SkCodec::FrameInfo::fRequiredFrame to the
332 // earliest frame which can be used. This might come up when several
333 // frames update the same subregion. If that same subregion is about to be
334 // overwritten, it doesn't matter which frame in that chain is provided.
335
336 DCHECK_LT(index, frame_buffer_cache_.size());
337 DCHECK(index);
338
339 size_t previous_reference_frame_index = kNotFound;
340 size_t required_previous_frame_index =
341 frame_buffer_cache_[index].RequiredPreviousFrameIndex();
342 if (required_previous_frame_index != kNotFound) {
343 for (size_t i = index - 1; i != required_previous_frame_index; i--) {
344 const ImageFrame& frame = frame_buffer_cache_[i];
345
346 if (frame.GetDisposalMethod() == ImageFrame::kDisposeOverwritePrevious)
347 continue;
348
349 if (frame.GetStatus() == ImageFrame::kFrameComplete) {
350 previous_reference_frame_index = i;
351 break;
352 }
353 }
354 }
355
356 return previous_reference_frame_index;
357 }
358
282 } // namespace blink 359 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698