Chromium Code Reviews| 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 |
| 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 codec_.reset(SkCodec::NewFromStream(segment_stream_, nullptr)); | |
| 64 if (!codec_) { | |
| 65 // |segment_stream_|'s ownership is passed into NewFromStream. | |
| 66 // It is deleted if NewFromStream fails. | |
| 67 // If NewFromStream fails, we set |segment_stream_| to null so | |
| 68 // we aren't pointing to reclaimed memory. | |
| 69 segment_stream_ = nullptr; | |
| 70 return; | |
| 71 } | |
| 72 | |
| 73 // SkCodec::NewFromStream will read enough of the image to get the image | |
| 74 // size. | |
| 75 SkImageInfo image_info = codec_->getInfo(); | |
| 76 SetSize(image_info.width(), image_info.height()); | |
| 77 } | |
| 46 } | 78 } |
| 47 | 79 |
| 48 int GIFImageDecoder::RepetitionCount() const { | 80 int GIFImageDecoder::RepetitionCount() const { |
| 81 if (!codec_) | |
| 82 return kAnimationLoopOnce; | |
| 83 | |
| 84 DCHECK(!Failed()); | |
| 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 kCLoopCountNotSeen, in which case we | |
| 54 // should default to looping once (the initial value for | |
| 55 // |repetition_count_|). | |
| 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 int repetition_count = codec_->getRepetitionCount(); |
| 60 // loop count. We don't need to special-case this because in this case the | 94 |
| 61 // new reader will once again return kCLoopCountNotSeen, and we won't | 95 switch (repetition_count) { |
| 62 // overwrite the cached correct value. | 96 case 0: { |
| 63 // | 97 size_t frame_count = codec_->getFrameCount(); |
| 64 // Second, a GIF might never set a loop count at all, in which case we | 98 if (IsAllDataReceived() && frame_count == 1) |
| 65 // should continue to treat it as a "loop once" animation. We don't need | 99 return kAnimationNone; |
| 66 // special code here either, because in this case we'll never change | 100 |
| 67 // |repetition_count_| from its default value. | 101 return kAnimationLoopOnce; |
| 68 // | 102 } |
| 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 kAnimationLoopInfinite; |
| 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_ = 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 } | 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 (static_cast<size_t>(codec_->getFrameCount()) <= index) | |
| 115 return false; | |
| 116 | |
| 117 SkCodec::FrameInfo frame_info; | |
| 118 codec_->getFrameInfo(index, &frame_info); | |
| 119 return frame_info.fFullyReceived; | |
| 85 } | 120 } |
| 86 | 121 |
| 87 float GIFImageDecoder::FrameDurationAtIndex(size_t index) const { | 122 float GIFImageDecoder::FrameDurationAtIndex(size_t index) const { |
| 88 return (reader_ && (index < reader_->ImagesCount()) && | 123 if (index < frame_buffer_cache_.size()) |
| 89 reader_->FrameContext(index)->IsHeaderDefined()) | 124 return frame_buffer_cache_[index].Duration(); |
| 90 ? reader_->FrameContext(index)->DelayTime() | 125 return 0; |
| 91 : 0; | |
| 92 } | 126 } |
| 93 | 127 |
| 94 bool GIFImageDecoder::SetFailed() { | 128 bool GIFImageDecoder::SetFailed() { |
| 95 reader_.reset(); | 129 DCHECK(codec_); |
| 130 | |
| 131 segment_stream_ = nullptr; | |
| 132 codec_ = nullptr; | |
| 96 return ImageDecoder::SetFailed(); | 133 return ImageDecoder::SetFailed(); |
| 97 } | 134 } |
| 98 | 135 |
| 99 bool GIFImageDecoder::HaveDecodedRow(size_t frame_index, | 136 size_t GIFImageDecoder::DecodeFrameCount() { |
| 100 GIFRow::const_iterator row_begin, | 137 if (!codec_) |
| 101 size_t width, | 138 return 0; |
| 102 size_t row_number, | 139 |
| 103 unsigned repeat_count, | 140 if (Failed() || segment_stream_->IsCleared()) |
| 104 bool write_transparent_pixels) { | 141 return 0; |
| 105 const GIFFrameContext* frame_context = reader_->FrameContext(frame_index); | 142 |
| 106 // The pixel data and coordinates supplied to us are relative to the frame's | 143 return codec_->getFrameCount(); |
| 107 // origin within the entire image size, i.e. | 144 } |
| 108 // (frameC_context->xOffset, frame_context->yOffset). There is no guarantee | 145 |
| 109 // that width == (size().width() - frame_context->xOffset), so | 146 void GIFImageDecoder::InitializeNewFrame(size_t index) { |
| 110 // we must ensure we don't run off the end of either the source data or the | 147 DCHECK(codec_); |
| 111 // row's X-coordinates. | 148 |
| 112 const int x_begin = frame_context->XOffset(); | 149 ImageFrame& frame = frame_buffer_cache_[index]; |
| 113 const int y_begin = frame_context->YOffset() + row_number; | 150 // SkCodec does not inform us if only a portion of the image was updated |
| 114 const int x_end = std::min(static_cast<int>(frame_context->XOffset() + width), | 151 // in the current frame. Because of this, rather than correctly filling in |
| 115 Size().Width()); | 152 // the frame rect, we set the frame rect to be the image's full size. |
| 116 const int y_end = std::min( | 153 // The original frame rect is not used, anyway. |
| 117 static_cast<int>(frame_context->YOffset() + row_number + repeat_count), | 154 IntSize full_image_size = Size(); |
| 118 Size().Height()); | 155 frame.SetOriginalFrameRect(IntRect(IntPoint(), full_image_size)); |
| 119 if (!width || (x_begin < 0) || (y_begin < 0) || (x_end <= x_begin) || | 156 |
| 120 (y_end <= y_begin)) | 157 SkCodec::FrameInfo frame_info; |
| 121 return true; | 158 codec_->getFrameInfo(index, &frame_info); |
| 122 | 159 frame.SetDuration(frame_info.fDuration); |
| 123 const GIFColorMap::Table& color_table = | 160 frame.SetHasAlpha(!SkAlphaTypeIsOpaque(frame_info.fAlphaType)); |
| 124 frame_context->LocalColorMap().IsDefined() | 161 size_t required_previous_frame_index; |
| 125 ? frame_context->LocalColorMap().GetTable() | 162 if (frame_info.fRequiredFrame == SkCodec::kNone) { |
| 126 : reader_->GlobalColorMap().GetTable(); | 163 required_previous_frame_index = WTF::kNotFound; |
| 127 | 164 } else { |
| 128 if (color_table.IsEmpty()) | 165 required_previous_frame_index = |
| 129 return true; | 166 static_cast<size_t>(frame_info.fRequiredFrame); |
| 130 | 167 } |
| 131 GIFColorMap::Table::const_iterator color_table_iter = color_table.begin(); | 168 frame.SetRequiredPreviousFrameIndex(required_previous_frame_index); |
| 132 | 169 |
| 133 // Initialize the frame if necessary. | 170 ImageFrame::DisposalMethod disposal_method = ImageFrame::kDisposeNotSpecified; |
| 134 ImageFrame& buffer = frame_buffer_cache_[frame_index]; | 171 switch (frame_info.fDisposalMethod) { |
| 135 if (!InitFrameBuffer(frame_index)) | 172 case SkCodecAnimation::DisposalMethod::kKeep: |
| 136 return false; | 173 disposal_method = ImageFrame::kDisposeKeep; |
| 137 | 174 break; |
| 138 const size_t transparent_pixel = frame_context->TransparentPixel(); | 175 case SkCodecAnimation::DisposalMethod::kRestoreBGColor: |
| 139 GIFRow::const_iterator row_end = row_begin + (x_end - x_begin); | 176 disposal_method = ImageFrame::kDisposeOverwriteBgcolor; |
| 140 ImageFrame::PixelData* current_address = buffer.GetAddr(x_begin, y_begin); | 177 break; |
| 141 | 178 case SkCodecAnimation::DisposalMethod::kRestorePrevious: |
| 142 // We may or may not need to write transparent pixels to the buffer. | 179 disposal_method = ImageFrame::kDisposeOverwritePrevious; |
| 143 // If we're compositing against a previous image, it's wrong, and if | 180 break; |
| 144 // we're writing atop a cleared, fully transparent buffer, it's | 181 } |
| 145 // unnecessary; but if we're decoding an interlaced gif and | 182 frame.SetDisposalMethod(disposal_method); |
| 146 // displaying it "Haeberli"-style, we must write these for passes | 183 } |
| 147 // beyond the first, or the initial passes will "show through" the | 184 |
| 148 // later ones. | 185 void GIFImageDecoder::Decode(size_t index) { |
| 149 // | 186 if (!codec_) |
| 150 // The loops below are almost identical. One writes a transparent pixel | 187 return; |
| 151 // and one doesn't based on the value of |write_transparent_pixels|. | 188 |
| 152 // The condition check is taken out of the loop to enhance performance. | 189 DCHECK(!Failed()); |
| 153 // This optimization reduces decoding time by about 15% for a 3MB image. | 190 |
| 154 if (write_transparent_pixels) { | 191 DCHECK_LT(index, frame_buffer_cache_.size()); |
| 155 for (; row_begin != row_end; ++row_begin, ++current_address) { | 192 |
| 156 const size_t source_value = *row_begin; | 193 if (segment_stream_->IsCleared()) |
| 157 if ((source_value != transparent_pixel) && | 194 return; |
| 158 (source_value < color_table.size())) { | 195 |
| 159 *current_address = color_table_iter[source_value]; | 196 UpdateAggressivePurging(index); |
| 160 } else { | 197 SkImageInfo image_info = codec_->getInfo() |
| 161 *current_address = 0; | 198 .makeColorType(kN32_SkColorType) |
| 162 current_buffer_saw_alpha_ = true; | 199 .makeColorSpace(ColorSpaceForSkImages()); |
| 200 | |
| 201 SkCodec::Options options; | |
| 202 options.fFrameIndex = index; | |
| 203 options.fPriorFrame = SkCodec::kNone; | |
| 204 options.fZeroInitialized = SkCodec::kYes_ZeroInitialized; | |
| 205 | |
| 206 ImageFrame& frame = frame_buffer_cache_[index]; | |
| 207 frame.ZeroFillPixelData(); | |
|
scroggo_chromium
2017/07/12 17:18:01
I'd recommend following the example of InitFrameBu
cblume
2017/07/14 08:38:18
Done.
| |
| 208 frame.SetPixelsChanged(true); | |
|
scroggo_chromium
2017/07/12 17:18:01
No need to call SetPixelsChanged, which should onl
cblume
2017/07/14 08:38:18
Done.
| |
| 209 if (frame.GetStatus() == ImageFrame::kFrameEmpty) { | |
| 210 size_t required_previous_frame_index = frame.RequiredPreviousFrameIndex(); | |
| 211 if (required_previous_frame_index == WTF::kNotFound) { | |
| 212 frame.AllocatePixelData(Size().Width(), Size().Height(), | |
| 213 ColorSpaceForSkImages()); | |
| 214 } else { | |
| 215 // Any frame in the range [required_previous_frame_index, index) which | |
| 216 // has a disposal method other than kRestorePrevious can be provided as | |
| 217 // the prior frame to SkCodec. | |
| 218 // | |
| 219 // This is because SkCodec sets SkCodec::FrameInfo::fRequiredFrame to the | |
| 220 // earliest frame which can be used. This might come up when several | |
| 221 // frames update the same subregion. If that same subregion is about to be | |
| 222 // overwritten, it doesn't matter which frame in that chain is provided. | |
| 223 size_t previous_frame_index = required_previous_frame_index; | |
| 224 for (size_t i = required_previous_frame_index; i < index; i++) { | |
| 225 ImageFrame& previous_frame = frame_buffer_cache_[i]; | |
| 226 | |
| 227 if (previous_frame.GetDisposalMethod() == | |
| 228 ImageFrame::kDisposeOverwritePrevious) | |
| 229 continue; | |
| 230 | |
| 231 if (previous_frame.GetStatus() == ImageFrame::kFrameComplete) { | |
| 232 previous_frame_index = i; | |
| 233 break; | |
| 234 } | |
| 163 } | 235 } |
| 164 } | 236 |
| 165 } else { | 237 ImageFrame& previous_frame = frame_buffer_cache_[previous_frame_index]; |
| 166 for (; row_begin != row_end; ++row_begin, ++current_address) { | 238 if (previous_frame.GetStatus() != ImageFrame::kFrameComplete) |
| 167 const size_t source_value = *row_begin; | 239 Decode(previous_frame_index); |
| 168 if ((source_value != transparent_pixel) && | 240 |
| 169 (source_value < color_table.size())) | 241 // We try to reuse |previous_frame| as starting state to avoid copying. |
| 170 *current_address = color_table_iter[source_value]; | 242 // If CanReusePreviousFrameBuffer returns false, we must copy the data |
| 171 else | 243 // since |previous_frame| is necessary to decode this or later frames. |
| 172 current_buffer_saw_alpha_ = true; | 244 // In that case copy the data instead. |
| 173 } | 245 if ((!CanReusePreviousFrameBuffer(index) || |
| 174 } | 246 !frame.TakeBitmapDataIfWritable(&previous_frame)) && |
| 175 | 247 !frame.CopyBitmapData(previous_frame)) { |
| 176 // Tell the frame to copy the row data if need be. | 248 SetFailed(); |
| 177 if (repeat_count > 1) | 249 return; |
| 178 buffer.CopyRowNTimes(x_begin, x_end, y_begin, y_end); | 250 } |
| 179 | 251 options.fPriorFrame = previous_frame_index; |
| 180 buffer.SetPixelsChanged(true); | 252 } |
| 181 return true; | 253 } |
| 182 } | 254 |
| 183 | 255 if (frame.GetStatus() == ImageFrame::kFrameAllocated) { |
| 184 bool GIFImageDecoder::ParseCompleted() const { | 256 SkCodec::Result start_incremental_decode_result = |
| 185 return reader_ && reader_->ParseCompleted(); | 257 codec_->startIncrementalDecode(image_info, frame.Bitmap().getPixels(), |
| 186 } | 258 frame.Bitmap().rowBytes(), &options, |
| 187 | 259 nullptr, nullptr); |
| 188 bool GIFImageDecoder::FrameComplete(size_t frame_index) { | 260 switch (start_incremental_decode_result) { |
| 189 // Initialize the frame if necessary. Some GIFs insert do-nothing frames, | 261 case SkCodec::kSuccess: |
| 190 // in which case we never reach HaveDecodedRow() before getting here. | 262 break; |
| 191 if (!InitFrameBuffer(frame_index)) | 263 case SkCodec::kIncompleteInput: |
| 192 return SetFailed(); | 264 return; |
| 193 | 265 default: |
| 194 if (!current_buffer_saw_alpha_) | 266 SetFailed(); |
| 195 CorrectAlphaWhenFrameBufferSawNoAlpha(frame_index); | 267 return; |
| 196 | 268 } |
| 197 frame_buffer_cache_[frame_index].SetStatus(ImageFrame::kFrameComplete); | 269 frame.SetStatus(ImageFrame::kFramePartial); |
| 198 | 270 } |
| 199 return true; | 271 int rows_decoded = 0; |
| 200 } | 272 SkCodec::Result incremental_decode_result = |
| 201 | 273 codec_->incrementalDecode(&rows_decoded); |
| 202 void GIFImageDecoder::ClearFrameBuffer(size_t frame_index) { | 274 switch (incremental_decode_result) { |
| 203 if (reader_ && frame_buffer_cache_[frame_index].GetStatus() == | 275 case SkCodec::kSuccess: |
| 204 ImageFrame::kFramePartial) { | 276 frame.SetPixelsChanged(true); |
| 205 // Reset the state of the partial frame in the reader so that the frame | 277 frame.SetStatus(ImageFrame::kFrameComplete); |
| 206 // can be decoded again when requested. | 278 PostDecodeProcessing(index); |
| 207 reader_->ClearDecodeState(frame_index); | 279 break; |
| 208 } | 280 case SkCodec::kIncompleteInput: |
| 209 ImageDecoder::ClearFrameBuffer(frame_index); | 281 frame.SetPixelsChanged(true); |
| 210 } | 282 if (FrameIsCompleteAtIndex(index) || IsAllDataReceived()) { |
| 211 | 283 SetFailed(); |
| 212 size_t GIFImageDecoder::DecodeFrameCount() { | 284 return; |
| 213 Parse(kGIFFrameCountQuery); | 285 } |
| 214 // If decoding fails, |reader_| will have been destroyed. Instead of | 286 |
| 215 // returning 0 in this case, return the existing number of frames. This way | 287 break; |
| 216 // if we get halfway through the image before decoding fails, we won't | 288 default: |
| 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(); | 289 SetFailed(); |
| 244 return; | 290 return; |
| 245 } | 291 } |
| 246 | 292 } |
| 247 // If this returns false, we need more data to continue decoding. | 293 |
| 248 if (!PostDecodeProcessing(*i)) | 294 bool GIFImageDecoder::CanReusePreviousFrameBuffer(size_t index) const { |
| 249 break; | 295 DCHECK(index < frame_buffer_cache_.size()); |
| 250 } | 296 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; | 297 ImageFrame::kDisposeOverwritePrevious; |
| 280 } | 298 } |
| 281 | 299 |
| 282 } // namespace blink | 300 } // namespace blink |
| OLD | NEW |