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 SkCodec::Result stream_creation_result; | |
64 codec_.reset(SkCodec::NewFromStream(segment_stream_, | |
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 SetFailed(); | |
83 return; | |
84 } | |
85 } | |
46 } | 86 } |
47 | 87 |
48 int GIFImageDecoder::RepetitionCount() const { | 88 int GIFImageDecoder::RepetitionCount() const { |
89 if (!codec_) | |
90 return kAnimationLoopOnce; | |
91 | |
92 DCHECK(!Failed()); | |
93 | |
49 // This value can arrive at any point in the image data stream. Most GIFs | 94 // 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 | 95 // 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 | 96 // 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 | 97 // 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 // | 98 // |
57 // There are some additional wrinkles here. First, ImageSource::Clear() | 99 // 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_ | 100 // seen yet. |
59 // authoritative on future calls if the recreated reader hasn't seen the | 101 int repetition_count = codec_->getRepetitionCount(); |
60 // loop count. We don't need to special-case this because in this case the | 102 |
61 // new reader will once again return kCLoopCountNotSeen, and we won't | 103 switch (repetition_count) { |
62 // overwrite the cached correct value. | 104 case 0: { |
63 // | 105 // SkCodec returns 0 for both still images and animated images which only |
64 // Second, a GIF might never set a loop count at all, in which case we | 106 // play once. |
65 // should continue to treat it as a "loop once" animation. We don't need | 107 size_t frame_count = codec_->getFrameCount(); |
66 // special code here either, because in this case we'll never change | 108 if (IsAllDataReceived() && frame_count == 1) |
scroggo_chromium
2017/07/18 21:10:10
nit: No need for this variable. This could be:
cblume
2017/07/19 23:27:05
Done.
| |
67 // |repetition_count_| from its default value. | 109 return kAnimationNone; |
68 // | 110 |
69 // Third, we use the same GIFImageReader for counting frames and we might | 111 return kAnimationLoopOnce; |
70 // see the loop count and then encounter a decoding error which happens | 112 } |
71 // later in the stream. It is also possible that no frames are in the | 113 case SkCodec::kRepetitionCountInfinite: |
72 // stream. In these cases we should just loop once. | 114 return kAnimationLoopInfinite; |
73 if (IsAllDataReceived() && ParseCompleted() && reader_->ImagesCount() == 1) | 115 default: |
74 repetition_count_ = kAnimationNone; | 116 return repetition_count; |
75 else if (Failed() || (reader_ && (!reader_->ImagesCount()))) | 117 } |
76 repetition_count_ = kAnimationLoopOnce; | |
77 else if (reader_ && reader_->LoopCount() != kCLoopCountNotSeen) | |
78 repetition_count_ = reader_->LoopCount(); | |
79 return repetition_count_; | |
80 } | 118 } |
81 | 119 |
82 bool GIFImageDecoder::FrameIsCompleteAtIndex(size_t index) const { | 120 bool GIFImageDecoder::FrameIsCompleteAtIndex(size_t index) const { |
83 return reader_ && (index < reader_->ImagesCount()) && | 121 if (!codec_) |
84 reader_->FrameContext(index)->IsComplete(); | 122 return false; |
123 | |
124 if (static_cast<size_t>(codec_->getFrameCount()) <= index) | |
125 return false; | |
126 | |
127 SkCodec::FrameInfo frame_info; | |
128 codec_->getFrameInfo(index, &frame_info); | |
129 return frame_info.fFullyReceived; | |
85 } | 130 } |
86 | 131 |
87 float GIFImageDecoder::FrameDurationAtIndex(size_t index) const { | 132 float GIFImageDecoder::FrameDurationAtIndex(size_t index) const { |
88 return (reader_ && (index < reader_->ImagesCount()) && | 133 if (index < frame_buffer_cache_.size()) |
89 reader_->FrameContext(index)->IsHeaderDefined()) | 134 return frame_buffer_cache_[index].Duration(); |
90 ? reader_->FrameContext(index)->DelayTime() | 135 return 0; |
91 : 0; | |
92 } | 136 } |
93 | 137 |
94 bool GIFImageDecoder::SetFailed() { | 138 bool GIFImageDecoder::SetFailed() { |
95 reader_.reset(); | 139 segment_stream_ = nullptr; |
140 codec_ = nullptr; | |
scroggo_chromium
2017/07/18 21:10:09
nit: I prefer
codec_.reset();
here. It's more
cblume
2017/07/19 23:27:05
I hadn't considered this before. I like it. Done.
| |
96 return ImageDecoder::SetFailed(); | 141 return ImageDecoder::SetFailed(); |
97 } | 142 } |
98 | 143 |
99 bool GIFImageDecoder::HaveDecodedRow(size_t frame_index, | 144 size_t GIFImageDecoder::ClearCacheExceptFrame(size_t index) { |
scroggo_chromium
2017/07/18 17:52:42
Since we're overriding this method, please remove
cblume
2017/07/19 23:27:05
Done.
| |
100 GIFRow::const_iterator row_begin, | 145 // SkCodec attempts to report the earliest possible required frame, but it is |
101 size_t width, | 146 // possible that frame has been evicted, while a later frame (which could also |
102 size_t row_number, | 147 // be used as the required frame) is still cached. Try to preserve a frame |
103 unsigned repeat_count, | 148 // that is still cached. |
104 bool write_transparent_pixels) { | 149 if (frame_buffer_cache_.size() <= 1) |
105 const GIFFrameContext* frame_context = reader_->FrameContext(frame_index); | 150 return 0; |
106 // The pixel data and coordinates supplied to us are relative to the frame's | 151 |
107 // origin within the entire image size, i.e. | 152 size_t index2 = kNotFound; |
108 // (frameC_context->xOffset, frame_context->yOffset). There is no guarantee | 153 if (index < frame_buffer_cache_.size()) { |
109 // that width == (size().width() - frame_context->xOffset), so | 154 const ImageFrame& frame = frame_buffer_cache_[index]; |
110 // we must ensure we don't run off the end of either the source data or the | 155 if (!FrameStatusSufficientForSuccessors(index) || |
111 // row's X-coordinates. | 156 frame.GetDisposalMethod() == ImageFrame::kDisposeOverwritePrevious) { |
112 const int x_begin = frame_context->XOffset(); | 157 // We need to preserve a previous frame |
scroggo_chromium
2017/07/18 21:10:09
This comment probably doesn't add much. (Tells a w
cblume
2017/07/19 23:27:04
Yeah, I agree. I think this comment is useless.
| |
113 const int y_begin = frame_context->YOffset() + row_number; | 158 index2 = GetViableReferenceFrameIndex(index); |
114 const int x_end = std::min(static_cast<int>(frame_context->XOffset() + width), | 159 } |
115 Size().Width()); | 160 } |
116 const int y_end = std::min( | 161 |
117 static_cast<int>(frame_context->YOffset() + row_number + repeat_count), | 162 return ClearCacheExceptTwoFrames(index, index2); |
118 Size().Height()); | 163 } |
119 if (!width || (x_begin < 0) || (y_begin < 0) || (x_end <= x_begin) || | 164 |
120 (y_end <= y_begin)) | 165 size_t GIFImageDecoder::DecodeFrameCount() { |
121 return true; | 166 if (!codec_) |
122 | 167 return 0; |
scroggo_chromium
2017/07/18 21:10:10
In the old code, we returned the existing number o
cblume
2017/07/19 23:27:05
Done.
| |
123 const GIFColorMap::Table& color_table = | 168 |
124 frame_context->LocalColorMap().IsDefined() | 169 if (Failed() || segment_stream_->IsCleared()) |
125 ? frame_context->LocalColorMap().GetTable() | 170 return 0; |
126 : reader_->GlobalColorMap().GetTable(); | 171 |
127 | 172 return codec_->getFrameCount(); |
128 if (color_table.IsEmpty()) | 173 } |
129 return true; | 174 |
130 | 175 void GIFImageDecoder::InitializeNewFrame(size_t index) { |
131 GIFColorMap::Table::const_iterator color_table_iter = color_table.begin(); | 176 DCHECK(codec_); |
132 | 177 |
133 // Initialize the frame if necessary. | 178 ImageFrame& frame = frame_buffer_cache_[index]; |
134 ImageFrame& buffer = frame_buffer_cache_[frame_index]; | 179 // SkCodec does not inform us if only a portion of the image was updated |
135 if (!InitFrameBuffer(frame_index)) | 180 // in the current frame. Because of this, rather than correctly filling in |
136 return false; | 181 // the frame rect, we set the frame rect to be the image's full size. |
137 | 182 // The original frame rect is not used, anyway. |
138 const size_t transparent_pixel = frame_context->TransparentPixel(); | 183 IntSize full_image_size = Size(); |
139 GIFRow::const_iterator row_end = row_begin + (x_end - x_begin); | 184 frame.SetOriginalFrameRect(IntRect(IntPoint(), full_image_size)); |
140 ImageFrame::PixelData* current_address = buffer.GetAddr(x_begin, y_begin); | 185 |
141 | 186 SkCodec::FrameInfo frame_info; |
142 // We may or may not need to write transparent pixels to the buffer. | 187 codec_->getFrameInfo(index, &frame_info); |
scroggo_chromium
2017/07/18 21:10:10
This method returns a boolean. We should probably
cblume
2017/07/19 23:27:05
Done.
| |
143 // If we're compositing against a previous image, it's wrong, and if | 188 frame.SetDuration(frame_info.fDuration); |
144 // we're writing atop a cleared, fully transparent buffer, it's | 189 frame.SetHasAlpha(!SkAlphaTypeIsOpaque(frame_info.fAlphaType)); |
145 // unnecessary; but if we're decoding an interlaced gif and | 190 size_t required_previous_frame_index; |
146 // displaying it "Haeberli"-style, we must write these for passes | 191 if (frame_info.fRequiredFrame == SkCodec::kNone) { |
147 // beyond the first, or the initial passes will "show through" the | 192 required_previous_frame_index = WTF::kNotFound; |
148 // later ones. | 193 } else { |
149 // | 194 required_previous_frame_index = |
150 // The loops below are almost identical. One writes a transparent pixel | 195 static_cast<size_t>(frame_info.fRequiredFrame); |
151 // and one doesn't based on the value of |write_transparent_pixels|. | 196 } |
152 // The condition check is taken out of the loop to enhance performance. | 197 frame.SetRequiredPreviousFrameIndex(required_previous_frame_index); |
153 // This optimization reduces decoding time by about 15% for a 3MB image. | 198 |
154 if (write_transparent_pixels) { | 199 ImageFrame::DisposalMethod disposal_method = ImageFrame::kDisposeNotSpecified; |
155 for (; row_begin != row_end; ++row_begin, ++current_address) { | 200 switch (frame_info.fDisposalMethod) { |
156 const size_t source_value = *row_begin; | 201 case SkCodecAnimation::DisposalMethod::kKeep: |
157 if ((source_value != transparent_pixel) && | 202 disposal_method = ImageFrame::kDisposeKeep; |
158 (source_value < color_table.size())) { | 203 break; |
159 *current_address = color_table_iter[source_value]; | 204 case SkCodecAnimation::DisposalMethod::kRestoreBGColor: |
160 } else { | 205 disposal_method = ImageFrame::kDisposeOverwriteBgcolor; |
161 *current_address = 0; | 206 break; |
162 current_buffer_saw_alpha_ = true; | 207 case SkCodecAnimation::DisposalMethod::kRestorePrevious: |
208 disposal_method = ImageFrame::kDisposeOverwritePrevious; | |
209 break; | |
210 } | |
211 frame.SetDisposalMethod(disposal_method); | |
212 } | |
213 | |
214 void GIFImageDecoder::Decode(size_t index) { | |
215 if (!codec_) | |
216 return; | |
217 | |
218 DCHECK(!Failed()); | |
219 | |
220 DCHECK_LT(index, frame_buffer_cache_.size()); | |
221 | |
222 if (segment_stream_->IsCleared()) | |
223 return; | |
224 | |
225 UpdateAggressivePurging(index); | |
226 SkImageInfo image_info = codec_->getInfo() | |
227 .makeColorType(kN32_SkColorType) | |
228 .makeColorSpace(ColorSpaceForSkImages()); | |
229 | |
230 SkCodec::Options options; | |
231 options.fFrameIndex = index; | |
232 options.fPriorFrame = SkCodec::kNone; | |
233 options.fZeroInitialized = SkCodec::kNo_ZeroInitialized; | |
234 | |
235 ImageFrame& frame = frame_buffer_cache_[index]; | |
236 if (frame.GetStatus() == ImageFrame::kFrameEmpty) { | |
237 size_t required_previous_frame_index = frame.RequiredPreviousFrameIndex(); | |
238 if (required_previous_frame_index == WTF::kNotFound) { | |
239 frame.AllocatePixelData(Size().Width(), Size().Height(), | |
240 ColorSpaceForSkImages()); | |
241 bool oldAlpha = frame.HasAlpha(); | |
scroggo_chromium
2017/07/18 21:10:09
I know we've gone back and forth on this, but mayb
cblume
2017/07/19 23:27:05
I'm pretty sure we tried SkAlphaTypeIsOpaque() bef
scroggo_chromium
2017/07/20 13:59:52
That doesn't make sense - SkAlphaTypeIsOpaque is h
cblume
2017/07/20 21:53:44
Oh sorry, I think I misread. I thought you meant u
| |
242 frame.ZeroFillPixelData(); | |
243 options.fZeroInitialized = SkCodec::kYes_ZeroInitialized; | |
244 frame.SetHasAlpha(oldAlpha); | |
245 } else { | |
246 size_t previous_frame_index = GetViableReferenceFrameIndex(index); | |
247 if (previous_frame_index == kNotFound) | |
248 previous_frame_index = required_previous_frame_index; | |
249 | |
250 ImageFrame& previous_frame = frame_buffer_cache_[previous_frame_index]; | |
scroggo_chromium
2017/07/18 21:10:10
It seems like this could be a part of the prior if
cblume
2017/07/19 23:27:05
Done.
| |
251 if (previous_frame.GetStatus() != ImageFrame::kFrameComplete) | |
252 Decode(previous_frame_index); | |
253 | |
254 // We try to reuse |previous_frame| as starting state to avoid copying. | |
255 // If CanReusePreviousFrameBuffer returns false, we must copy the data | |
256 // since |previous_frame| is necessary to decode this or later frames. | |
257 // In that case copy the data instead. | |
258 if ((!CanReusePreviousFrameBuffer(index) || | |
259 !frame.TakeBitmapDataIfWritable(&previous_frame)) && | |
260 !frame.CopyBitmapData(previous_frame)) { | |
261 SetFailed(); | |
262 return; | |
163 } | 263 } |
164 } | 264 options.fPriorFrame = previous_frame_index; |
165 } else { | 265 } |
166 for (; row_begin != row_end; ++row_begin, ++current_address) { | 266 } |
167 const size_t source_value = *row_begin; | 267 |
168 if ((source_value != transparent_pixel) && | 268 if (frame.GetStatus() == ImageFrame::kFrameAllocated) { |
scroggo_chromium
2017/07/18 21:10:10
It took me a while to get on board with kFrameAllo
cblume
2017/07/19 23:27:05
:)
I would be happy to adopt a simpler way if we k
| |
169 (source_value < color_table.size())) | 269 SkCodec::Result start_incremental_decode_result = |
170 *current_address = color_table_iter[source_value]; | 270 codec_->startIncrementalDecode(image_info, frame.Bitmap().getPixels(), |
171 else | 271 frame.Bitmap().rowBytes(), &options); |
172 current_buffer_saw_alpha_ = true; | 272 switch (start_incremental_decode_result) { |
173 } | 273 case SkCodec::kSuccess: |
174 } | 274 break; |
175 | 275 case SkCodec::kIncompleteInput: |
176 // Tell the frame to copy the row data if need be. | 276 return; |
177 if (repeat_count > 1) | 277 default: |
178 buffer.CopyRowNTimes(x_begin, x_end, y_begin, y_end); | 278 SetFailed(); |
179 | 279 return; |
180 buffer.SetPixelsChanged(true); | 280 } |
181 return true; | 281 frame.SetStatus(ImageFrame::kFramePartial); |
182 } | 282 } |
scroggo_chromium
2017/07/18 21:10:10
nit: Add a newline after this? That way we separat
cblume
2017/07/19 23:27:04
Done.
| |
183 | 283 int rows_decoded = 0; |
scroggo_chromium
2017/07/18 21:10:10
Now that we zero pixels ahead of time, this variab
cblume
2017/07/19 23:27:05
Done.
| |
184 bool GIFImageDecoder::ParseCompleted() const { | 284 SkCodec::Result incremental_decode_result = |
185 return reader_ && reader_->ParseCompleted(); | 285 codec_->incrementalDecode(&rows_decoded); |
186 } | 286 switch (incremental_decode_result) { |
187 | 287 case SkCodec::kSuccess: |
188 bool GIFImageDecoder::FrameComplete(size_t frame_index) { | 288 frame.SetPixelsChanged(true); |
189 // Initialize the frame if necessary. Some GIFs insert do-nothing frames, | 289 frame.SetStatus(ImageFrame::kFrameComplete); |
190 // in which case we never reach HaveDecodedRow() before getting here. | 290 PostDecodeProcessing(index); |
191 if (!InitFrameBuffer(frame_index)) | 291 break; |
192 return SetFailed(); | 292 case SkCodec::kIncompleteInput: |
193 | 293 frame.SetPixelsChanged(true); |
194 if (!current_buffer_saw_alpha_) | 294 if (FrameIsCompleteAtIndex(index) || IsAllDataReceived()) { |
195 CorrectAlphaWhenFrameBufferSawNoAlpha(frame_index); | 295 SetFailed(); |
196 | 296 return; |
scroggo_chromium
2017/07/18 21:10:10
These returns feel arbitrary to me. If you left th
cblume
2017/07/19 23:27:05
This was actually intentional, but I'm happy to re
| |
197 frame_buffer_cache_[frame_index].SetStatus(ImageFrame::kFrameComplete); | 297 } |
198 | 298 |
199 return true; | 299 break; |
200 } | 300 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(); | 301 SetFailed(); |
244 return; | 302 return; |
scroggo_chromium
2017/07/18 21:10:10
I support putting either a break or a return here,
cblume
2017/07/19 23:27:05
Done.
| |
245 } | 303 } |
246 | 304 } |
247 // If this returns false, we need more data to continue decoding. | 305 |
248 if (!PostDecodeProcessing(*i)) | 306 bool GIFImageDecoder::CanReusePreviousFrameBuffer(size_t index) const { |
249 break; | 307 DCHECK(index < frame_buffer_cache_.size()); |
250 } | 308 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; | 309 ImageFrame::kDisposeOverwritePrevious; |
280 } | 310 } |
281 | 311 |
312 size_t GIFImageDecoder::GetViableReferenceFrameIndex( | |
313 size_t dependent_index) const { | |
314 size_t required_previous_frame_index = | |
315 frame_buffer_cache_[dependent_index].RequiredPreviousFrameIndex(); | |
316 // Any frame in the range [|required_previous_frame_index|, |dependent_index|) | |
317 // which has a disposal method other than kRestorePrevious can be provided as | |
318 // the prior frame to SkCodec. | |
319 // | |
320 // SkCodec sets SkCodec::FrameInfo::fRequiredFrame to the earliest frame which | |
321 // can be used. This might come up when several frames update the same | |
322 // subregion. If that same subregion is about to be overwritten, it doesn't | |
323 // matter which frame in that chain is provided. | |
324 | |
325 DCHECK_LT(dependent_index, frame_buffer_cache_.size()); | |
326 DCHECK(dependent_index); | |
scroggo_chromium
2017/07/18 17:52:42
I think this DCHECK might fire in the following si
cblume
2017/07/19 23:27:05
I think you are right. I think we can drop it.
Fr
| |
327 | |
328 size_t previous_reference_frame_index = kNotFound; | |
329 if (required_previous_frame_index != kNotFound) { | |
330 for (size_t i = dependent_index - 1; i != required_previous_frame_index; | |
scroggo_chromium
2017/07/18 21:10:10
Specify why we go in reverse?
cblume
2017/07/19 23:27:05
Done.
I put in a fairly general comment rather tha
| |
331 i--) { | |
332 const ImageFrame& frame = frame_buffer_cache_[i]; | |
333 | |
334 if (frame.GetDisposalMethod() == ImageFrame::kDisposeOverwritePrevious) | |
335 continue; | |
336 | |
337 if (frame.GetStatus() == ImageFrame::kFrameComplete) { | |
338 previous_reference_frame_index = i; | |
339 break; | |
340 } | |
341 } | |
342 } | |
343 | |
344 return previous_reference_frame_index; | |
345 } | |
346 | |
282 } // namespace blink | 347 } // namespace blink |
OLD | NEW |