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 "platform/image-decoders/gif/GIFImageReader.h" | 28 #include "third_party/skia/include/core/SkImageInfo.h" |
29 #include "wtf/NotFound.h" | 29 #include "wtf/NotFound.h" |
30 #include "wtf/PtrUtil.h" | 30 #include "wtf/PtrUtil.h" |
31 #include <limits> | 31 #include <limits> |
32 | 32 |
33 namespace blink { | 33 namespace blink { |
34 | 34 |
35 GIFImageDecoder::GIFImageDecoder(AlphaOption alphaOption, | 35 GIFImageDecoder::GIFImageDecoder(AlphaOption alphaOption, |
36 const ColorBehavior& colorBehavior, | 36 const ColorBehavior& colorBehavior, |
37 size_t maxDecodedBytes) | 37 size_t maxDecodedBytes) |
38 : ImageDecoder(alphaOption, colorBehavior, maxDecodedBytes), | 38 : ImageDecoder(alphaOption, colorBehavior, maxDecodedBytes), |
39 m_repetitionCount(cAnimationLoopOnce) {} | 39 m_codec(), |
40 | 40 m_segmentStream(nullptr) {} |
41 GIFImageDecoder::~GIFImageDecoder() {} | 41 |
42 GIFImageDecoder::~GIFImageDecoder() { | |
43 if (!m_codec) { | |
44 // if we did not create m_codec and thus did not pass ownership to it | |
45 if (m_segmentStream) { | |
46 delete m_segmentStream; | |
47 } | |
48 } | |
49 } | |
42 | 50 |
43 void GIFImageDecoder::onSetData(SegmentReader* data) { | 51 void GIFImageDecoder::onSetData(SegmentReader* data) { |
44 if (m_reader) | 52 if (!m_segmentStream) { |
45 m_reader->setData(data); | 53 m_segmentStream = new SegmentStream(); |
54 } | |
55 | |
56 m_segmentStream->setReader(data, isAllDataReceived()); | |
57 | |
58 // If we don't have a SkCodec yet, create one from the stream | |
59 if (!m_codec) { | |
60 m_codec.reset(SkCodec::NewFromStream(m_segmentStream)); | |
61 if (!m_codec) { | |
62 // m_segmentStream's ownership is passed. It is deleted if SkCodec | |
63 // creation fails. In this case, release our reference so we can create a | |
64 // new SegmentStream later. | |
65 m_segmentStream = nullptr; | |
66 } | |
67 } | |
46 } | 68 } |
47 | 69 |
48 int GIFImageDecoder::repetitionCount() const { | 70 int GIFImageDecoder::repetitionCount() const { |
49 // This value can arrive at any point in the image data stream. Most GIFs | 71 // 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 | 72 // 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 | 73 // 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 | 74 // 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 // | 75 // |
57 // There are some additional wrinkles here. First, ImageSource::clear() | 76 // 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_ | 77 // seen yet. |
59 // authoritative on future calls if the recreated reader hasn't seen the | 78 |
60 // loop count. We don't need to special-case this because in this case the | 79 int repetitionCount = m_codec->getRepetitionCount(); |
scroggo_chromium
2016/12/19 14:28:54
It looks like you removed a line that says
CHEC
cblume
2016/12/20 21:59:00
Done.
| |
61 // new reader will once again return cLoopCountNotSeen, and we won't | 80 switch (repetitionCount) { |
62 // overwrite the cached correct value. | 81 case 0: |
63 // | 82 return cAnimationNone; |
64 // Second, a GIF might never set a loop count at all, in which case we | 83 case SkCodec::kRepetitionCountInfinite: |
65 // should continue to treat it as a "loop once" animation. We don't need | 84 return cAnimationLoopInfinite; |
66 // special code here either, because in this case we'll never change | 85 default: |
67 // |m_repetitionCount| from its default value. | 86 return repetitionCount; |
68 // | 87 } |
69 // Third, we use the same GIFImageReader for counting frames and we might | |
70 // see the loop count and then encounter a decoding error which happens | |
71 // later in the stream. It is also possible that no frames are in the | |
72 // stream. In these cases we should just loop once. | |
73 if (isAllDataReceived() && parseCompleted() && m_reader->imagesCount() == 1) | |
74 m_repetitionCount = cAnimationNone; | |
75 else if (failed() || (m_reader && (!m_reader->imagesCount()))) | |
76 m_repetitionCount = cAnimationLoopOnce; | |
77 else if (m_reader && m_reader->loopCount() != cLoopCountNotSeen) | |
78 m_repetitionCount = m_reader->loopCount(); | |
79 return m_repetitionCount; | |
80 } | 88 } |
81 | 89 |
82 bool GIFImageDecoder::frameIsCompleteAtIndex(size_t index) const { | 90 bool GIFImageDecoder::frameIsCompleteAtIndex(size_t index) const { |
83 return m_reader && (index < m_reader->imagesCount()) && | 91 std::vector<SkCodec::FrameInfo> frameInfos = m_codec->getFrameInfo(); |
84 m_reader->frameContext(index)->isComplete(); | 92 if (frameInfos.size() <= index) { |
93 return false; | |
94 } | |
95 | |
96 return frameInfos[index].fFullyReceived; | |
85 } | 97 } |
86 | 98 |
87 float GIFImageDecoder::frameDurationAtIndex(size_t index) const { | 99 float GIFImageDecoder::frameDurationAtIndex(size_t index) const { |
88 return (m_reader && (index < m_reader->imagesCount()) && | 100 std::vector<SkCodec::FrameInfo> frameInfos = m_codec->getFrameInfo(); |
89 m_reader->frameContext(index)->isHeaderDefined()) | 101 if (frameInfos.size() <= index) { |
90 ? m_reader->frameContext(index)->delayTime() | 102 return 0; |
91 : 0; | 103 } |
92 } | 104 |
93 | 105 return frameInfos[index].fDuration; |
94 bool GIFImageDecoder::setFailed() { | 106 } |
95 m_reader.reset(); | 107 |
96 return ImageDecoder::setFailed(); | 108 void GIFImageDecoder::decodeSize() { |
97 } | 109 SkImageInfo imageInfo = m_codec->getInfo(); |
98 | 110 setSize(imageInfo.width(), imageInfo.height()); |
99 bool GIFImageDecoder::haveDecodedRow(size_t frameIndex, | 111 } |
100 GIFRow::const_iterator rowBegin, | 112 |
101 size_t width, | 113 size_t GIFImageDecoder::decodeFrameCount() { |
102 size_t rowNumber, | 114 std::vector<SkCodec::FrameInfo> frameInfos = m_codec->getFrameInfo(); |
103 unsigned repeatCount, | 115 return frameInfos.size(); |
104 bool writeTransparentPixels) { | 116 } |
105 const GIFFrameContext* frameContext = m_reader->frameContext(frameIndex); | 117 |
106 // The pixel data and coordinates supplied to us are relative to the frame's | 118 void GIFImageDecoder::initializeNewFrame(size_t index) { |
107 // origin within the entire image size, i.e. | 119 ImageFrame& frame = m_frameBufferCache[index]; |
108 // (frameContext->xOffset, frameContext->yOffset). There is no guarantee | 120 std::vector<SkCodec::FrameInfo> frameInfos = m_codec->getFrameInfo(); |
109 // that width == (size().width() - frameContext->xOffset), so | 121 |
110 // we must ensure we don't run off the end of either the source data or the | 122 IntSize frameSize = size(); |
scroggo_chromium
2016/12/19 14:28:54
Maybe a FIXME here that we need to verify that we
cblume
2016/12/20 21:59:00
Done.
| |
111 // row's X-coordinates. | 123 frame.setOriginalFrameRect(IntRect(IntPoint(), frameSize)); |
112 const int xBegin = frameContext->xOffset(); | 124 frame.setSizeAndColorSpace(frameSize.width(), frameSize.height(), |
113 const int yBegin = frameContext->yOffset() + rowNumber; | 125 colorSpaceForSkImages()); |
114 const int xEnd = std::min(static_cast<int>(frameContext->xOffset() + width), | 126 frame.setDuration(frameInfos[index].fDuration); |
115 size().width()); | 127 // The disposal method is not required any more, but is left in place |
116 const int yEnd = std::min( | 128 // for the other image decoders that do not yet rely on SkCodec. |
117 static_cast<int>(frameContext->yOffset() + rowNumber + repeatCount), | 129 // For now, fill it with DisposeKeep. |
118 size().height()); | 130 frame.setDisposalMethod(ImageFrame::DisposeKeep); |
119 if (!width || (xBegin < 0) || (yBegin < 0) || (xEnd <= xBegin) || | 131 size_t requiredPreviousFrame = frameInfos[index].fRequiredFrame; |
120 (yEnd <= yBegin)) | 132 if (requiredPreviousFrame == SkCodec::kNone) { |
121 return true; | 133 requiredPreviousFrame = WTF::kNotFound; |
122 | 134 } |
123 const GIFColorMap::Table& colorTable = | 135 frame.setRequiredPreviousFrameIndex(requiredPreviousFrame); |
124 frameContext->localColorMap().isDefined() | 136 } |
125 ? frameContext->localColorMap().getTable() | 137 |
126 : m_reader->globalColorMap().getTable(); | 138 void GIFImageDecoder::decode(size_t index) { |
127 | 139 if (m_frameBufferCache.size() <= index) { |
128 if (colorTable.isEmpty()) | 140 // It is a fatal error if all data is received and we have decoded all |
129 return true; | 141 // frames available but the file is truncated. |
130 | 142 if (isAllDataReceived()) { |
131 GIFColorMap::Table::const_iterator colorTableIter = colorTable.begin(); | 143 setFailed(); |
132 | 144 } |
133 // Initialize the frame if necessary. | 145 |
134 ImageFrame& buffer = m_frameBufferCache[frameIndex]; | 146 return; |
135 if (!initFrameBuffer(frameIndex)) | 147 } |
136 return false; | 148 |
137 | 149 updateAggressivePurging(index); |
138 const size_t transparentPixel = frameContext->transparentPixel(); | 150 |
139 GIFRow::const_iterator rowEnd = rowBegin + (xEnd - xBegin); | 151 SkImageInfo imageInfo = m_codec->getInfo().makeColorType(kN32_SkColorType); |
140 ImageFrame::PixelData* currentAddress = buffer.getAddr(xBegin, yBegin); | 152 |
141 | 153 SkCodec::Options options; |
142 // We may or may not need to write transparent pixels to the buffer. | 154 options.fFrameIndex = index; |
143 // If we're compositing against a previous image, it's wrong, and if | 155 options.fHasPriorFrame = false; |
144 // we're writing atop a cleared, fully transparent buffer, it's | 156 |
145 // unnecessary; but if we're decoding an interlaced gif and | 157 ImageFrame& frame = m_frameBufferCache[index]; |
146 // displaying it "Haeberli"-style, we must write these for passes | 158 if (frame.getStatus() == ImageFrame::FrameEmpty) { |
147 // beyond the first, or the initial passes will "show through" the | 159 size_t requiredPreviousFrameIndex = frame.requiredPreviousFrameIndex(); |
148 // later ones. | 160 if (requiredPreviousFrameIndex != WTF::kNotFound) { |
149 // | 161 options.fHasPriorFrame = true; |
150 // The loops below are almost identical. One writes a transparent pixel | 162 } |
151 // and one doesn't based on the value of |writeTransparentPixels|. | 163 |
152 // The condition check is taken out of the loop to enhance performance. | 164 SkCodec::Result startIncrementalDecodeResult = |
153 // This optimization reduces decoding time by about 15% for a 3MB image. | 165 m_codec->startIncrementalDecode(imageInfo, frame.bitmap().getPixels(), |
154 if (writeTransparentPixels) { | 166 frame.bitmap().rowBytes(), &options, |
155 for (; rowBegin != rowEnd; ++rowBegin, ++currentAddress) { | 167 nullptr, nullptr); |
156 const size_t sourceValue = *rowBegin; | 168 switch (startIncrementalDecodeResult) { |
157 if ((sourceValue != transparentPixel) && | 169 case SkCodec::kSuccess: |
158 (sourceValue < colorTable.size())) { | 170 break; |
159 *currentAddress = colorTableIter[sourceValue]; | 171 case SkCodec::kIncompleteInput: |
172 return; | |
173 default: | |
174 setFailed(); | |
175 return; | |
176 } | |
177 | |
178 if (requiredPreviousFrameIndex != WTF::kNotFound) { | |
179 ImageFrame& requiredPreviousFrame = | |
180 m_frameBufferCache[requiredPreviousFrameIndex]; | |
181 | |
182 if (canReusePreviousFrameBuffer(index)) { | |
183 if (!frame.takeBitmapDataIfWritable(&requiredPreviousFrame)) { | |
184 setFailed(); | |
scroggo_chromium
2016/12/19 14:28:54
This isn't quite right. Look at where these method
cblume
2016/12/20 21:59:00
Done.
| |
185 return; | |
186 } | |
160 } else { | 187 } else { |
161 *currentAddress = 0; | 188 if (!frame.copyBitmapData(requiredPreviousFrame)) { |
162 m_currentBufferSawAlpha = true; | 189 setFailed(); |
190 return; | |
191 } | |
163 } | 192 } |
164 } | 193 } |
165 } else { | 194 } |
166 for (; rowBegin != rowEnd; ++rowBegin, ++currentAddress) { | 195 |
167 const size_t sourceValue = *rowBegin; | 196 int rowsDecoded = 0; |
168 if ((sourceValue != transparentPixel) && | 197 SkCodec::Result incrementalDecodeResult = |
169 (sourceValue < colorTable.size())) | 198 m_codec->incrementalDecode(&rowsDecoded); |
170 *currentAddress = colorTableIter[sourceValue]; | 199 switch (incrementalDecodeResult) { |
171 else | 200 case SkCodec::kSuccess: |
172 m_currentBufferSawAlpha = true; | 201 frame.setStatus(ImageFrame::FrameComplete); |
173 } | 202 break; |
174 } | 203 case SkCodec::kIncompleteInput: |
175 | 204 if (frame.getStatus() == ImageFrame::FrameEmpty && index == 0) { |
176 // Tell the frame to copy the row data if need be. | 205 // We want to display the bit of the frame we have decoded only if it is |
177 if (repeatCount > 1) | 206 // the first frame of an animation. That means we need to fill the rest |
178 buffer.copyRowNTimes(xBegin, xEnd, yBegin, yEnd); | 207 // of the image with transparent. |
179 | 208 IntRect remainingRect = frame.originalFrameRect(); |
180 buffer.setPixelsChanged(true); | 209 remainingRect.setHeight(remainingRect.height() - rowsDecoded); |
181 return true; | 210 frame.zeroFillFrameRect(remainingRect); |
182 } | 211 } |
183 | 212 |
184 bool GIFImageDecoder::parseCompleted() const { | 213 frame.setStatus(ImageFrame::FramePartial); |
185 return m_reader && m_reader->parseCompleted(); | 214 break; |
186 } | 215 default: |
187 | |
188 bool GIFImageDecoder::frameComplete(size_t frameIndex) { | |
189 // Initialize the frame if necessary. Some GIFs insert do-nothing frames, | |
190 // in which case we never reach haveDecodedRow() before getting here. | |
191 if (!initFrameBuffer(frameIndex)) | |
192 return false; // initFrameBuffer() has already called setFailed(). | |
193 | |
194 m_frameBufferCache[frameIndex].setStatus(ImageFrame::FrameComplete); | |
195 if (!m_currentBufferSawAlpha) | |
196 correctAlphaWhenFrameBufferSawNoAlpha(frameIndex); | |
197 | |
198 return true; | |
199 } | |
200 | |
201 void GIFImageDecoder::clearFrameBuffer(size_t frameIndex) { | |
202 if (m_reader && | |
203 m_frameBufferCache[frameIndex].getStatus() == ImageFrame::FramePartial) { | |
204 // Reset the state of the partial frame in the reader so that the frame | |
205 // can be decoded again when requested. | |
206 m_reader->clearDecodeState(frameIndex); | |
207 } | |
208 ImageDecoder::clearFrameBuffer(frameIndex); | |
209 } | |
210 | |
211 size_t GIFImageDecoder::decodeFrameCount() { | |
212 parse(GIFFrameCountQuery); | |
213 // If decoding fails, |m_reader| will have been destroyed. Instead of | |
214 // returning 0 in this case, return the existing number of frames. This way | |
215 // if we get halfway through the image before decoding fails, we won't | |
216 // suddenly start reporting that the image has zero frames. | |
217 return failed() ? m_frameBufferCache.size() : m_reader->imagesCount(); | |
218 } | |
219 | |
220 void GIFImageDecoder::initializeNewFrame(size_t index) { | |
221 ImageFrame* buffer = &m_frameBufferCache[index]; | |
222 const GIFFrameContext* frameContext = m_reader->frameContext(index); | |
223 buffer->setOriginalFrameRect( | |
224 intersection(frameContext->frameRect(), IntRect(IntPoint(), size()))); | |
225 buffer->setDuration(frameContext->delayTime()); | |
226 buffer->setDisposalMethod(frameContext->getDisposalMethod()); | |
227 buffer->setRequiredPreviousFrameIndex( | |
228 findRequiredPreviousFrame(index, false)); | |
229 } | |
230 | |
231 void GIFImageDecoder::decode(size_t index) { | |
232 parse(GIFFrameCountQuery); | |
233 | |
234 if (failed()) | |
235 return; | |
236 | |
237 updateAggressivePurging(index); | |
238 | |
239 Vector<size_t> framesToDecode = findFramesToDecode(index); | |
240 for (auto i = framesToDecode.rbegin(); i != framesToDecode.rend(); ++i) { | |
241 if (!m_reader->decode(*i)) { | |
242 setFailed(); | 216 setFailed(); |
243 return; | 217 return; |
244 } | 218 } |
245 | 219 |
246 // If this returns false, we need more data to continue decoding. | 220 if (!postDecodeProcessing(index)) { |
247 if (!postDecodeProcessing(*i)) | |
248 break; | |
249 } | |
250 | |
251 // It is also a fatal error if all data is received and we have decoded all | |
252 // frames available but the file is truncated. | |
253 if (index >= m_frameBufferCache.size() - 1 && isAllDataReceived() && | |
254 m_reader && !m_reader->parseCompleted()) | |
255 setFailed(); | |
256 } | |
257 | |
258 void GIFImageDecoder::parse(GIFParseQuery query) { | |
259 if (failed()) | |
260 return; | 221 return; |
261 | 222 } |
262 if (!m_reader) { | |
263 m_reader = WTF::makeUnique<GIFImageReader>(this); | |
264 m_reader->setData(m_data); | |
265 } | |
266 | |
267 if (!m_reader->parse(query)) | |
268 setFailed(); | |
269 } | |
270 | |
271 void GIFImageDecoder::onInitFrameBuffer(size_t frameIndex) { | |
272 m_currentBufferSawAlpha = false; | |
273 } | 223 } |
274 | 224 |
275 bool GIFImageDecoder::canReusePreviousFrameBuffer(size_t frameIndex) const { | 225 bool GIFImageDecoder::canReusePreviousFrameBuffer(size_t frameIndex) const { |
276 DCHECK(frameIndex < m_frameBufferCache.size()); | 226 DCHECK(frameIndex < m_frameBufferCache.size()); |
227 // TODO Without knowing the disposal method of a frame (which you see copied | |
228 // below from the old GIFImageDecoder), I would need to rebuild frame | |
229 // dependencies. | |
230 // | |
231 // One option after we loop once and determine if a frame is referenced by a | |
232 // later frame. The goal here is to not reuse a buffer that will be needed | |
233 // again later. | |
cblume
2016/12/19 08:04:03
Ideally, we would only take (reuse) the bitmap on
scroggo_chromium
2016/12/19 14:28:54
No, I do not think that's the behavior that we wan
| |
277 return m_frameBufferCache[frameIndex].getDisposalMethod() != | 234 return m_frameBufferCache[frameIndex].getDisposalMethod() != |
278 ImageFrame::DisposeOverwritePrevious; | 235 ImageFrame::DisposeOverwritePrevious; |
279 } | 236 } |
280 | 237 |
281 } // namespace blink | 238 } // namespace blink |
OLD | NEW |