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