Chromium Code Reviews| Index: third_party/WebKit/Source/platform/image-decoders/gif/GIFImageDecoder.cpp | 
| diff --git a/third_party/WebKit/Source/platform/image-decoders/gif/GIFImageDecoder.cpp b/third_party/WebKit/Source/platform/image-decoders/gif/GIFImageDecoder.cpp | 
| index b954a982e6fbc2decfe0c22117ab6573de011dc8..b2eea188f7c53e6ed2c42fa30b8b63defb7df32f 100644 | 
| --- a/third_party/WebKit/Source/platform/image-decoders/gif/GIFImageDecoder.cpp | 
| +++ b/third_party/WebKit/Source/platform/image-decoders/gif/GIFImageDecoder.cpp | 
| @@ -26,9 +26,9 @@ | 
| #include "platform/image-decoders/gif/GIFImageDecoder.h" | 
| #include <limits> | 
| -#include "platform/image-decoders/gif/GIFImageReader.h" | 
| #include "platform/wtf/NotFound.h" | 
| #include "platform/wtf/PtrUtil.h" | 
| +#include "third_party/skia/include/core/SkImageInfo.h" | 
| namespace blink { | 
| @@ -36,247 +36,324 @@ GIFImageDecoder::GIFImageDecoder(AlphaOption alpha_option, | 
| const ColorBehavior& color_behavior, | 
| size_t max_decoded_bytes) | 
| : ImageDecoder(alpha_option, color_behavior, max_decoded_bytes), | 
| - repetition_count_(kAnimationLoopOnce) {} | 
| - | 
| -GIFImageDecoder::~GIFImageDecoder() {} | 
| + codec_(), | 
| + segment_stream_(nullptr) {} | 
| + | 
| +GIFImageDecoder::~GIFImageDecoder() { | 
| + if (!codec_) { | 
| + // if we did not create |codec_| and thus did not pass ownership to it | 
| + if (segment_stream_) | 
| + delete segment_stream_; | 
| + } | 
| +} | 
| void GIFImageDecoder::OnSetData(SegmentReader* data) { | 
| - if (reader_) | 
| - reader_->SetData(data); | 
| + if (!data) { | 
| + if (segment_stream_) | 
| + segment_stream_->SetReader(nullptr); | 
| + return; | 
| + } | 
| + | 
| + if (!segment_stream_) | 
| + segment_stream_ = new SegmentStream(); | 
| + | 
| + segment_stream_->SetReader(PassRefPtr<SegmentReader>(data)); | 
| + | 
| + if (!codec_) { | 
| + SkCodec::Result stream_creation_result; | 
| + 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
 
 | 
| + &stream_creation_result, nullptr)); | 
| + switch (stream_creation_result) { | 
| + case SkCodec::kSuccess: { | 
| + // SkCodec::NewFromStream will read enough of the image to get the image | 
| + // size. | 
| + SkImageInfo image_info = codec_->getInfo(); | 
| + SetSize(image_info.width(), image_info.height()); | 
| + return; | 
| + } | 
| + case SkCodec::kIncompleteInput: | 
| + // |segment_stream_|'s ownership is passed into NewFromStream. | 
| + // It is deleted if NewFromStream fails. | 
| + // If NewFromStream fails, we set |segment_stream_| to null so | 
| + // we aren't pointing to reclaimed memory. | 
| + segment_stream_ = nullptr; | 
| + return; | 
| + default: | 
| + segment_stream_ = nullptr; | 
| 
 
vmpstr
2017/07/17 21:28:40
nit: SetFailed will do this?
 
cblume
2017/07/18 10:23:49
Done.
 
 | 
| + 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
 
 | 
| + return; | 
| + } | 
| + } | 
| } | 
| int GIFImageDecoder::RepetitionCount() const { | 
| + if (!codec_) | 
| + return kAnimationLoopOnce; | 
| + | 
| + DCHECK(!Failed()); | 
| + | 
| // This value can arrive at any point in the image data stream. Most GIFs | 
| // in the wild declare it near the beginning of the file, so it usually is | 
| // set by the time we've decoded the size, but (depending on the GIF and the | 
| - // packets sent back by the webserver) not always. If the reader hasn't | 
| - // seen a loop count yet, it will return kCLoopCountNotSeen, in which case we | 
| - // should default to looping once (the initial value for | 
| - // |repetition_count_|). | 
| - // | 
| - // There are some additional wrinkles here. First, ImageSource::Clear() | 
| - // may destroy the reader, making the result from the reader _less_ | 
| - // authoritative on future calls if the recreated reader hasn't seen the | 
| - // loop count. We don't need to special-case this because in this case the | 
| - // new reader will once again return kCLoopCountNotSeen, and we won't | 
| - // overwrite the cached correct value. | 
| + // packets sent back by the webserver) not always. | 
| // | 
| - // Second, a GIF might never set a loop count at all, in which case we | 
| - // should continue to treat it as a "loop once" animation. We don't need | 
| - // special code here either, because in this case we'll never change | 
| - // |repetition_count_| from its default value. | 
| - // | 
| - // Third, we use the same GIFImageReader for counting frames and we might | 
| - // see the loop count and then encounter a decoding error which happens | 
| - // later in the stream. It is also possible that no frames are in the | 
| - // stream. In these cases we should just loop once. | 
| - if (IsAllDataReceived() && ParseCompleted() && reader_->ImagesCount() == 1) | 
| - repetition_count_ = kAnimationNone; | 
| - else if (Failed() || (reader_ && (!reader_->ImagesCount()))) | 
| - repetition_count_ = kAnimationLoopOnce; | 
| - else if (reader_ && reader_->LoopCount() != kCLoopCountNotSeen) | 
| - repetition_count_ = reader_->LoopCount(); | 
| - return repetition_count_; | 
| + // SkCodec will parse forward in the file if the repetition count has not been | 
| + // seen yet. | 
| + int repetition_count = codec_->getRepetitionCount(); | 
| + | 
| + switch (repetition_count) { | 
| + 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.
 
 | 
| + size_t frame_count = codec_->getFrameCount(); | 
| + if (IsAllDataReceived() && frame_count == 1) | 
| + return kAnimationNone; | 
| + | 
| + return kAnimationLoopOnce; | 
| + } | 
| + case SkCodec::kRepetitionCountInfinite: | 
| + return kAnimationLoopInfinite; | 
| + default: | 
| + return repetition_count; | 
| + } | 
| } | 
| bool GIFImageDecoder::FrameIsCompleteAtIndex(size_t index) const { | 
| - return reader_ && (index < reader_->ImagesCount()) && | 
| - reader_->FrameContext(index)->IsComplete(); | 
| + if (!codec_) | 
| + return false; | 
| + | 
| + if (static_cast<size_t>(codec_->getFrameCount()) <= index) | 
| + return false; | 
| + | 
| + SkCodec::FrameInfo frame_info; | 
| + codec_->getFrameInfo(index, &frame_info); | 
| + return frame_info.fFullyReceived; | 
| } | 
| float GIFImageDecoder::FrameDurationAtIndex(size_t index) const { | 
| - return (reader_ && (index < reader_->ImagesCount()) && | 
| - reader_->FrameContext(index)->IsHeaderDefined()) | 
| - ? reader_->FrameContext(index)->DelayTime() | 
| - : 0; | 
| + if (index < frame_buffer_cache_.size()) | 
| + return frame_buffer_cache_[index].Duration(); | 
| + return 0; | 
| } | 
| bool GIFImageDecoder::SetFailed() { | 
| - reader_.reset(); | 
| + DCHECK(codec_); | 
| + | 
| + segment_stream_ = nullptr; | 
| + codec_ = nullptr; | 
| return ImageDecoder::SetFailed(); | 
| } | 
| -bool GIFImageDecoder::HaveDecodedRow(size_t frame_index, | 
| - GIFRow::const_iterator row_begin, | 
| - size_t width, | 
| - size_t row_number, | 
| - unsigned repeat_count, | 
| - bool write_transparent_pixels) { | 
| - const GIFFrameContext* frame_context = reader_->FrameContext(frame_index); | 
| - // The pixel data and coordinates supplied to us are relative to the frame's | 
| - // origin within the entire image size, i.e. | 
| - // (frameC_context->xOffset, frame_context->yOffset). There is no guarantee | 
| - // that width == (size().width() - frame_context->xOffset), so | 
| - // we must ensure we don't run off the end of either the source data or the | 
| - // row's X-coordinates. | 
| - const int x_begin = frame_context->XOffset(); | 
| - const int y_begin = frame_context->YOffset() + row_number; | 
| - const int x_end = std::min(static_cast<int>(frame_context->XOffset() + width), | 
| - Size().Width()); | 
| - const int y_end = std::min( | 
| - static_cast<int>(frame_context->YOffset() + row_number + repeat_count), | 
| - Size().Height()); | 
| - if (!width || (x_begin < 0) || (y_begin < 0) || (x_end <= x_begin) || | 
| - (y_end <= y_begin)) | 
| - return true; | 
| - | 
| - const GIFColorMap::Table& color_table = | 
| - frame_context->LocalColorMap().IsDefined() | 
| - ? frame_context->LocalColorMap().GetTable() | 
| - : reader_->GlobalColorMap().GetTable(); | 
| - | 
| - if (color_table.IsEmpty()) | 
| - return true; | 
| - | 
| - GIFColorMap::Table::const_iterator color_table_iter = color_table.begin(); | 
| - | 
| - // Initialize the frame if necessary. | 
| - ImageFrame& buffer = frame_buffer_cache_[frame_index]; | 
| - if (!InitFrameBuffer(frame_index)) | 
| - return false; | 
| - | 
| - const size_t transparent_pixel = frame_context->TransparentPixel(); | 
| - GIFRow::const_iterator row_end = row_begin + (x_end - x_begin); | 
| - ImageFrame::PixelData* current_address = buffer.GetAddr(x_begin, y_begin); | 
| - | 
| - // We may or may not need to write transparent pixels to the buffer. | 
| - // If we're compositing against a previous image, it's wrong, and if | 
| - // we're writing atop a cleared, fully transparent buffer, it's | 
| - // unnecessary; but if we're decoding an interlaced gif and | 
| - // displaying it "Haeberli"-style, we must write these for passes | 
| - // beyond the first, or the initial passes will "show through" the | 
| - // later ones. | 
| +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
 
 | 
| + // SkCodec marks a required previous frame as the first frame in | 
| + // [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.
 
 | 
| + // 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.
 
 | 
| // | 
| - // The loops below are almost identical. One writes a transparent pixel | 
| - // and one doesn't based on the value of |write_transparent_pixels|. | 
| - // The condition check is taken out of the loop to enhance performance. | 
| - // This optimization reduces decoding time by about 15% for a 3MB image. | 
| - if (write_transparent_pixels) { | 
| - for (; row_begin != row_end; ++row_begin, ++current_address) { | 
| - const size_t source_value = *row_begin; | 
| - if ((source_value != transparent_pixel) && | 
| - (source_value < color_table.size())) { | 
| - *current_address = color_table_iter[source_value]; | 
| - } else { | 
| - *current_address = 0; | 
| - current_buffer_saw_alpha_ = true; | 
| - } | 
| - } | 
| - } else { | 
| - for (; row_begin != row_end; ++row_begin, ++current_address) { | 
| - const size_t source_value = *row_begin; | 
| - if ((source_value != transparent_pixel) && | 
| - (source_value < color_table.size())) | 
| - *current_address = color_table_iter[source_value]; | 
| - else | 
| - current_buffer_saw_alpha_ = true; | 
| + // 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.
 
 | 
| + // |index| is kDisposePrevious. | 
| + // | 
| + // 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
 
 | 
| + // not keep the marked required previous frame in the case of | 
| + // 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
 
 | 
| + // like other Blink decoders do. | 
| + if (frame_buffer_cache_.size() <= 1) | 
| + return 0; | 
| + | 
| + size_t index2 = kNotFound; | 
| + if (index < frame_buffer_cache_.size()) { | 
| + const ImageFrame& frame = frame_buffer_cache_[index]; | 
| + 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
 
 | 
| + frame.GetDisposalMethod() == ImageFrame::kDisposeOverwritePrevious) { | 
| + // We need to preserve a previous frame | 
| + index2 = GetPreviousReferenceFrame(index); | 
| } | 
| } | 
| - // Tell the frame to copy the row data if need be. | 
| - if (repeat_count > 1) | 
| - buffer.CopyRowNTimes(x_begin, x_end, y_begin, y_end); | 
| - | 
| - buffer.SetPixelsChanged(true); | 
| - return true; | 
| -} | 
| + while (index2 < frame_buffer_cache_.size() && | 
| + !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
 
 | 
| + index2 = GetPreviousReferenceFrame(index2); | 
| + } | 
| -bool GIFImageDecoder::ParseCompleted() const { | 
| - return reader_ && reader_->ParseCompleted(); | 
| + return ClearCacheExceptTwoFrames(index, index2); | 
| } | 
| -bool GIFImageDecoder::FrameComplete(size_t frame_index) { | 
| - // Initialize the frame if necessary. Some GIFs insert do-nothing frames, | 
| - // in which case we never reach HaveDecodedRow() before getting here. | 
| - if (!InitFrameBuffer(frame_index)) | 
| - return SetFailed(); | 
| - | 
| - if (!current_buffer_saw_alpha_) | 
| - CorrectAlphaWhenFrameBufferSawNoAlpha(frame_index); | 
| +size_t GIFImageDecoder::DecodeFrameCount() { | 
| + if (!codec_) | 
| + return 0; | 
| - frame_buffer_cache_[frame_index].SetStatus(ImageFrame::kFrameComplete); | 
| + if (Failed() || segment_stream_->IsCleared()) | 
| + return 0; | 
| - return true; | 
| + return codec_->getFrameCount(); | 
| } | 
| -void GIFImageDecoder::ClearFrameBuffer(size_t frame_index) { | 
| - if (reader_ && frame_buffer_cache_[frame_index].GetStatus() == | 
| - ImageFrame::kFramePartial) { | 
| - // Reset the state of the partial frame in the reader so that the frame | 
| - // can be decoded again when requested. | 
| - reader_->ClearDecodeState(frame_index); | 
| +void GIFImageDecoder::InitializeNewFrame(size_t index) { | 
| + DCHECK(codec_); | 
| + | 
| + ImageFrame& frame = frame_buffer_cache_[index]; | 
| + // SkCodec does not inform us if only a portion of the image was updated | 
| + // in the current frame. Because of this, rather than correctly filling in | 
| + // the frame rect, we set the frame rect to be the image's full size. | 
| + // The original frame rect is not used, anyway. | 
| + IntSize full_image_size = Size(); | 
| + frame.SetOriginalFrameRect(IntRect(IntPoint(), full_image_size)); | 
| + | 
| + SkCodec::FrameInfo frame_info; | 
| + codec_->getFrameInfo(index, &frame_info); | 
| + frame.SetDuration(frame_info.fDuration); | 
| + frame.SetHasAlpha(!SkAlphaTypeIsOpaque(frame_info.fAlphaType)); | 
| + size_t required_previous_frame_index; | 
| + if (frame_info.fRequiredFrame == SkCodec::kNone) { | 
| + required_previous_frame_index = WTF::kNotFound; | 
| + } else { | 
| + required_previous_frame_index = | 
| + static_cast<size_t>(frame_info.fRequiredFrame); | 
| } | 
| - ImageDecoder::ClearFrameBuffer(frame_index); | 
| -} | 
| + frame.SetRequiredPreviousFrameIndex(required_previous_frame_index); | 
| -size_t GIFImageDecoder::DecodeFrameCount() { | 
| - Parse(kGIFFrameCountQuery); | 
| - // If decoding fails, |reader_| will have been destroyed. Instead of | 
| - // returning 0 in this case, return the existing number of frames. This way | 
| - // if we get halfway through the image before decoding fails, we won't | 
| - // suddenly start reporting that the image has zero frames. | 
| - return Failed() ? frame_buffer_cache_.size() : reader_->ImagesCount(); | 
| -} | 
| - | 
| -void GIFImageDecoder::InitializeNewFrame(size_t index) { | 
| - ImageFrame* buffer = &frame_buffer_cache_[index]; | 
| - const GIFFrameContext* frame_context = reader_->FrameContext(index); | 
| - buffer->SetOriginalFrameRect( | 
| - Intersection(frame_context->FrameRect(), IntRect(IntPoint(), Size()))); | 
| - buffer->SetDuration(frame_context->DelayTime()); | 
| - buffer->SetDisposalMethod(frame_context->GetDisposalMethod()); | 
| - buffer->SetRequiredPreviousFrameIndex( | 
| - FindRequiredPreviousFrame(index, false)); | 
| + ImageFrame::DisposalMethod disposal_method = ImageFrame::kDisposeNotSpecified; | 
| + switch (frame_info.fDisposalMethod) { | 
| + case SkCodecAnimation::DisposalMethod::kKeep: | 
| + disposal_method = ImageFrame::kDisposeKeep; | 
| + break; | 
| + case SkCodecAnimation::DisposalMethod::kRestoreBGColor: | 
| + disposal_method = ImageFrame::kDisposeOverwriteBgcolor; | 
| + break; | 
| + case SkCodecAnimation::DisposalMethod::kRestorePrevious: | 
| + disposal_method = ImageFrame::kDisposeOverwritePrevious; | 
| + break; | 
| + } | 
| + frame.SetDisposalMethod(disposal_method); | 
| + 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.
 
 | 
| } | 
| void GIFImageDecoder::Decode(size_t index) { | 
| - Parse(kGIFFrameCountQuery); | 
| + if (!codec_) | 
| + return; | 
| + | 
| + DCHECK(!Failed()); | 
| - if (Failed()) | 
| + DCHECK_LT(index, frame_buffer_cache_.size()); | 
| + | 
| + if (segment_stream_->IsCleared()) | 
| return; | 
| UpdateAggressivePurging(index); | 
| + SkImageInfo image_info = codec_->getInfo() | 
| + .makeColorType(kN32_SkColorType) | 
| + .makeColorSpace(ColorSpaceForSkImages()); | 
| + | 
| + SkCodec::Options options; | 
| + options.fFrameIndex = index; | 
| + options.fPriorFrame = SkCodec::kNone; | 
| + options.fZeroInitialized = SkCodec::kNo_ZeroInitialized; | 
| + | 
| + ImageFrame& frame = frame_buffer_cache_[index]; | 
| + if (frame.GetStatus() == ImageFrame::kFrameEmpty) { | 
| + size_t required_previous_frame_index = frame.RequiredPreviousFrameIndex(); | 
| + if (required_previous_frame_index == WTF::kNotFound) { | 
| + frame.AllocatePixelData(Size().Width(), Size().Height(), | 
| + ColorSpaceForSkImages()); | 
| + bool oldAlpha = frame.HasAlpha(); | 
| + frame.ZeroFillPixelData(); | 
| + options.fZeroInitialized = SkCodec::kYes_ZeroInitialized; | 
| + frame.SetHasAlpha(oldAlpha); | 
| + } else { | 
| + size_t previous_frame_index = GetPreviousReferenceFrame(index); | 
| + if (previous_frame_index == kNotFound) | 
| + previous_frame_index = required_previous_frame_index; | 
| + | 
| + ImageFrame& previous_frame = frame_buffer_cache_[previous_frame_index]; | 
| + if (previous_frame.GetStatus() != ImageFrame::kFrameComplete) | 
| + Decode(previous_frame_index); | 
| + | 
| + // We try to reuse |previous_frame| as starting state to avoid copying. | 
| + // If CanReusePreviousFrameBuffer returns false, we must copy the data | 
| + // since |previous_frame| is necessary to decode this or later frames. | 
| + // In that case copy the data instead. | 
| + if ((!CanReusePreviousFrameBuffer(index) || | 
| + !frame.TakeBitmapDataIfWritable(&previous_frame)) && | 
| + !frame.CopyBitmapData(previous_frame)) { | 
| + SetFailed(); | 
| + return; | 
| + } | 
| + options.fPriorFrame = previous_frame_index; | 
| + } | 
| + } | 
| - Vector<size_t> frames_to_decode = FindFramesToDecode(index); | 
| - for (auto i = frames_to_decode.rbegin(); i != frames_to_decode.rend(); ++i) { | 
| - if (!reader_->Decode(*i)) { | 
| - SetFailed(); | 
| - return; | 
| + if (frame.GetStatus() == ImageFrame::kFrameAllocated) { | 
| + SkCodec::Result start_incremental_decode_result = | 
| + codec_->startIncrementalDecode(image_info, frame.Bitmap().getPixels(), | 
| + frame.Bitmap().rowBytes(), &options); | 
| + switch (start_incremental_decode_result) { | 
| + case SkCodec::kSuccess: | 
| + break; | 
| + case SkCodec::kIncompleteInput: | 
| + return; | 
| + default: | 
| + SetFailed(); | 
| + return; | 
| } | 
| + frame.SetStatus(ImageFrame::kFramePartial); | 
| + } | 
| + int rows_decoded = 0; | 
| + SkCodec::Result incremental_decode_result = | 
| + codec_->incrementalDecode(&rows_decoded); | 
| + switch (incremental_decode_result) { | 
| + case SkCodec::kSuccess: | 
| + frame.SetPixelsChanged(true); | 
| + frame.SetStatus(ImageFrame::kFrameComplete); | 
| + PostDecodeProcessing(index); | 
| + break; | 
| + case SkCodec::kIncompleteInput: | 
| + frame.SetPixelsChanged(true); | 
| + if (FrameIsCompleteAtIndex(index) || IsAllDataReceived()) { | 
| + SetFailed(); | 
| + return; | 
| + } | 
| - // If this returns false, we need more data to continue decoding. | 
| - if (!PostDecodeProcessing(*i)) | 
| break; | 
| + default: | 
| + SetFailed(); | 
| + return; | 
| } | 
| - | 
| - // It is also a fatal error if all data is received and we have decoded all | 
| - // frames available but the file is truncated. | 
| - if (index >= frame_buffer_cache_.size() - 1 && IsAllDataReceived() && | 
| - reader_ && !reader_->ParseCompleted()) | 
| - SetFailed(); | 
| } | 
| -void GIFImageDecoder::Parse(GIFParseQuery query) { | 
| - if (Failed()) | 
| - return; | 
| - | 
| - if (!reader_) { | 
| - reader_ = WTF::MakeUnique<GIFImageReader>(this); | 
| - reader_->SetData(data_); | 
| - } | 
| - | 
| - if (!reader_->Parse(query)) | 
| - SetFailed(); | 
| +bool GIFImageDecoder::CanReusePreviousFrameBuffer(size_t index) const { | 
| + DCHECK(index < frame_buffer_cache_.size()); | 
| + return frame_buffer_cache_[index].GetDisposalMethod() != | 
| + ImageFrame::kDisposeOverwritePrevious; | 
| } | 
| -void GIFImageDecoder::OnInitFrameBuffer(size_t frame_index) { | 
| - current_buffer_saw_alpha_ = false; | 
| -} | 
| +size_t GIFImageDecoder::GetPreviousReferenceFrame(size_t index) const { | 
| + // 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
 
 | 
| + // has a disposal method other than kRestorePrevious can be provided as | 
| + // the prior frame to SkCodec. | 
| + // | 
| + // This is because SkCodec sets SkCodec::FrameInfo::fRequiredFrame to the | 
| + // earliest frame which can be used. This might come up when several | 
| + // frames update the same subregion. If that same subregion is about to be | 
| + // overwritten, it doesn't matter which frame in that chain is provided. | 
| + | 
| + DCHECK_LT(index, frame_buffer_cache_.size()); | 
| + DCHECK(index); | 
| + | 
| + size_t previous_reference_frame_index = kNotFound; | 
| + size_t required_previous_frame_index = | 
| + frame_buffer_cache_[index].RequiredPreviousFrameIndex(); | 
| + if (required_previous_frame_index != kNotFound) { | 
| + for (size_t i = index - 1; i != required_previous_frame_index; i--) { | 
| + const ImageFrame& frame = frame_buffer_cache_[i]; | 
| + | 
| + if (frame.GetDisposalMethod() == ImageFrame::kDisposeOverwritePrevious) | 
| + continue; | 
| + | 
| + if (frame.GetStatus() == ImageFrame::kFrameComplete) { | 
| + previous_reference_frame_index = i; | 
| + break; | 
| + } | 
| + } | 
| + } | 
| -bool GIFImageDecoder::CanReusePreviousFrameBuffer(size_t frame_index) const { | 
| - DCHECK(frame_index < frame_buffer_cache_.size()); | 
| - return frame_buffer_cache_[frame_index].GetDisposalMethod() != | 
| - ImageFrame::kDisposeOverwritePrevious; | 
| + return previous_reference_frame_index; | 
| } | 
| } // namespace blink |