Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(654)

Side by Side Diff: third_party/WebKit/Source/platform/image-decoders/gif/GIFImageDecoder.cpp

Issue 2565323003: Move gif image decoder to SkCodec (Closed)
Patch Set: Fixing the alpha, not changing frame between init and decode. Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 (!data) {
45 m_reader->setData(data); 53 return;
54 }
55
56 if (!m_segmentStream) {
57 m_segmentStream = new SegmentStream();
58 }
59
60 if (!m_segmentStream) {
61 return;
62 }
63
64 m_segmentStream->setReader(data, isAllDataReceived());
65
66 // If we don't have a SkCodec yet, create one from the stream
67 if (!m_codec) {
68 SkCodec* codec = SkCodec::NewFromStream(m_segmentStream);
69 if (codec) {
70 m_codec.reset(codec);
71 } else {
72 // m_segmentStream's ownership is passed. It is deleted if SkCodec
73 // creation fails. In this case, release our reference so we can create a
74 // new SegmentStream later.
75 m_segmentStream = nullptr;
76 return;
77 }
78
79 // SkCodec::NewFromStream will read enough of the image to get the image
80 // size.
81 SkImageInfo imageInfo = m_codec->getInfo();
82 setSize(imageInfo.width(), imageInfo.height());
83 }
46 } 84 }
47 85
48 int GIFImageDecoder::repetitionCount() const { 86 int GIFImageDecoder::repetitionCount() const {
87 if (!m_codec) {
88 return 0;
89 }
90
49 // This value can arrive at any point in the image data stream. Most GIFs 91 // 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 92 // 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 93 // 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 94 // 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 // 95 //
57 // There are some additional wrinkles here. First, ImageSource::clear() 96 // 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_ 97 // seen yet.
59 // authoritative on future calls if the recreated reader hasn't seen the 98
60 // loop count. We don't need to special-case this because in this case the 99 int repetitionCount = m_codec->getRepetitionCount();
61 // new reader will once again return cLoopCountNotSeen, and we won't 100 switch (repetitionCount) {
62 // overwrite the cached correct value. 101 case 0:
63 // 102 return cAnimationNone;
64 // Second, a GIF might never set a loop count at all, in which case we 103 case SkCodec::kRepetitionCountInfinite:
65 // should continue to treat it as a "loop once" animation. We don't need 104 return cAnimationLoopInfinite;
66 // special code here either, because in this case we'll never change 105 default:
67 // |m_repetitionCount| from its default value. 106 return repetitionCount;
68 // 107 }
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 } 108 }
81 109
82 bool GIFImageDecoder::frameIsCompleteAtIndex(size_t index) const { 110 bool GIFImageDecoder::frameIsCompleteAtIndex(size_t index) const {
83 return m_reader && (index < m_reader->imagesCount()) && 111 if (!m_codec) {
84 m_reader->frameContext(index)->isComplete(); 112 return false;
113 }
114
115 std::vector<SkCodec::FrameInfo> frameInfos = m_codec->getFrameInfo();
116 if (frameInfos.size() <= index) {
117 return false;
118 }
119
120 return frameInfos[index].fFullyReceived;
85 } 121 }
86 122
87 float GIFImageDecoder::frameDurationAtIndex(size_t index) const { 123 float GIFImageDecoder::frameDurationAtIndex(size_t index) const {
88 return (m_reader && (index < m_reader->imagesCount()) && 124 if (!m_codec) {
89 m_reader->frameContext(index)->isHeaderDefined()) 125 return 0;
90 ? m_reader->frameContext(index)->delayTime() 126 }
91 : 0; 127
92 } 128 std::vector<SkCodec::FrameInfo> frameInfos = m_codec->getFrameInfo();
93 129 if (frameInfos.size() <= index) {
94 bool GIFImageDecoder::setFailed() { 130 return 0;
95 m_reader.reset(); 131 }
96 return ImageDecoder::setFailed(); 132
97 } 133 return frameInfos[index].fDuration;
98 134 }
99 bool GIFImageDecoder::haveDecodedRow(size_t frameIndex, 135
100 GIFRow::const_iterator rowBegin, 136 size_t GIFImageDecoder::decodeFrameCount() {
101 size_t width, 137 if (!m_codec) {
102 size_t rowNumber, 138 // We haven't yet successfully created the codec. Assume only 1 frame.
103 unsigned repeatCount, 139 return 1;
104 bool writeTransparentPixels) { 140 }
105 const GIFFrameContext* frameContext = m_reader->frameContext(frameIndex); 141
106 // The pixel data and coordinates supplied to us are relative to the frame's 142 std::vector<SkCodec::FrameInfo> frameInfos = m_codec->getFrameInfo();
107 // origin within the entire image size, i.e. 143 return frameInfos.size();
108 // (frameContext->xOffset, frameContext->yOffset). There is no guarantee 144 }
109 // that width == (size().width() - frameContext->xOffset), so 145
110 // we must ensure we don't run off the end of either the source data or the 146 void GIFImageDecoder::initializeNewFrame(size_t index) {
111 // row's X-coordinates. 147 if (!m_codec) {
112 const int xBegin = frameContext->xOffset(); 148 return;
113 const int yBegin = frameContext->yOffset() + rowNumber; 149 }
114 const int xEnd = std::min(static_cast<int>(frameContext->xOffset() + width), 150
115 size().width()); 151 ImageFrame& frame = m_frameBufferCache[index];
116 const int yEnd = std::min( 152 std::vector<SkCodec::FrameInfo> frameInfos = m_codec->getFrameInfo();
117 static_cast<int>(frameContext->yOffset() + rowNumber + repeatCount), 153
118 size().height()); 154 // FIXME We may not need to fill in all this frame information.
119 if (!width || (xBegin < 0) || (yBegin < 0) || (xEnd <= xBegin) || 155 // SkCodec doesn't need it. Check if Blink uses it at all.
120 (yEnd <= yBegin)) 156 // If Blink does use it, maybe SkCodec can provide it to us.
121 return true; 157 IntSize frameSize = size();
122 158 frame.setOriginalFrameRect(IntRect(IntPoint(), frameSize));
123 const GIFColorMap::Table& colorTable = 159 frame.setSizeAndColorSpace(frameSize.width(), frameSize.height(),
124 frameContext->localColorMap().isDefined() 160 colorSpaceForSkImages());
125 ? frameContext->localColorMap().getTable() 161 frame.setDuration(frameInfos[index].fDuration);
126 : m_reader->globalColorMap().getTable(); 162 // The disposal method is not required any more, but is left in place
127 163 // for the other image decoders that do not yet rely on SkCodec.
128 if (colorTable.isEmpty()) 164 // For now, fill it with DisposeKeep.
129 return true; 165 frame.setDisposalMethod(ImageFrame::DisposeKeep);
130 166 size_t requiredPreviousFrame = frameInfos[index].fRequiredFrame;
131 GIFColorMap::Table::const_iterator colorTableIter = colorTable.begin(); 167 if (requiredPreviousFrame == SkCodec::kNone) {
132 168 requiredPreviousFrame = WTF::kNotFound;
133 // Initialize the frame if necessary. 169 }
134 ImageFrame& buffer = m_frameBufferCache[frameIndex]; 170 frame.setRequiredPreviousFrameIndex(requiredPreviousFrame);
135 if (!initFrameBuffer(frameIndex)) 171 }
136 return false; 172
137 173 void GIFImageDecoder::decode(size_t index) {
138 const size_t transparentPixel = frameContext->transparentPixel(); 174 if (failed()) {
139 GIFRow::const_iterator rowEnd = rowBegin + (xEnd - xBegin); 175 return;
140 ImageFrame::PixelData* currentAddress = buffer.getAddr(xBegin, yBegin); 176 }
cblume 2017/01/09 04:11:22 The old decoder would exit early when failed() was
141 177
142 // We may or may not need to write transparent pixels to the buffer. 178 if (!m_codec) {
143 // If we're compositing against a previous image, it's wrong, and if 179 return;
144 // we're writing atop a cleared, fully transparent buffer, it's 180 }
145 // unnecessary; but if we're decoding an interlaced gif and 181
146 // displaying it "Haeberli"-style, we must write these for passes 182 if (m_frameBufferCache.size() <= index) {
147 // beyond the first, or the initial passes will "show through" the 183 // It is a fatal error if all data is received and we have decoded all
148 // later ones. 184 // frames available but the file is truncated.
149 // 185 if (isAllDataReceived()) {
150 // The loops below are almost identical. One writes a transparent pixel 186 setFailed();
151 // and one doesn't based on the value of |writeTransparentPixels|. 187 }
152 // The condition check is taken out of the loop to enhance performance. 188
153 // This optimization reduces decoding time by about 15% for a 3MB image. 189 return;
154 if (writeTransparentPixels) { 190 }
155 for (; rowBegin != rowEnd; ++rowBegin, ++currentAddress) { 191
156 const size_t sourceValue = *rowBegin; 192 updateAggressivePurging(index);
157 if ((sourceValue != transparentPixel) && 193
158 (sourceValue < colorTable.size())) { 194 SkImageInfo imageInfo = m_codec->getInfo().makeColorType(kN32_SkColorType);
159 *currentAddress = colorTableIter[sourceValue]; 195
160 } else { 196 ImageFrame& frame = m_frameBufferCache[index];
161 *currentAddress = 0; 197 size_t requiredPreviousFrameIndex = frame.requiredPreviousFrameIndex();
162 m_currentBufferSawAlpha = true; 198
199 SkCodec::Options options;
200 options.fFrameIndex = index;
201 options.fHasPriorFrame = false;
202 if (requiredPreviousFrameIndex != WTF::kNotFound) {
203 options.fHasPriorFrame = true;
204 }
205
206 if (frame.getStatus() == ImageFrame::FrameEmpty) {
207 if (requiredPreviousFrameIndex != WTF::kNotFound) {
208 ImageFrame& requiredPreviousFrame =
209 m_frameBufferCache[requiredPreviousFrameIndex];
210
211 // We try to reuse |requiredPreviousFrame| as starting state to avoid
212 // copying. If canReusePreviousFrameBuffer returns false, we must copy
213 // the data since |requiredPreviousFrame| is necessary to decode this
214 // or later frames. In that case copy the data instead.
215 if ((!canReusePreviousFrameBuffer(index) ||
216 !frame.takeBitmapDataIfWritable(&requiredPreviousFrame)) &&
217 !frame.copyBitmapData(requiredPreviousFrame)) {
218 setFailed();
219 return;
163 } 220 }
164 } 221 }
cblume 2017/01/09 04:11:22 I moved this logic up, above the call to startIncr
165 } else { 222
166 for (; rowBegin != rowEnd; ++rowBegin, ++currentAddress) { 223 SkCodec::Result startIncrementalDecodeResult =
167 const size_t sourceValue = *rowBegin; 224 m_codec->startIncrementalDecode(imageInfo, frame.bitmap().getPixels(),
168 if ((sourceValue != transparentPixel) && 225 frame.bitmap().rowBytes(), &options,
169 (sourceValue < colorTable.size())) 226 nullptr, nullptr);
170 *currentAddress = colorTableIter[sourceValue]; 227 switch (startIncrementalDecodeResult) {
171 else 228 case SkCodec::kSuccess:
172 m_currentBufferSawAlpha = true; 229 break;
173 } 230 case SkCodec::kIncompleteInput:
174 } 231 return;
175 232 default:
176 // Tell the frame to copy the row data if need be. 233 setFailed();
177 if (repeatCount > 1) 234 return;
178 buffer.copyRowNTimes(xBegin, xEnd, yBegin, yEnd); 235 }
179 236 }
180 buffer.setPixelsChanged(true); 237
181 return true; 238 int rowsDecoded = 0;
182 } 239 SkCodec::Result incrementalDecodeResult =
183 240 m_codec->incrementalDecode(&rowsDecoded);
184 bool GIFImageDecoder::parseCompleted() const { 241 switch (incrementalDecodeResult) {
185 return m_reader && m_reader->parseCompleted(); 242 case SkCodec::kSuccess:
186 } 243 frame.setStatus(ImageFrame::FrameComplete);
187 244 frame.setPixelsChanged(true);
188 bool GIFImageDecoder::frameComplete(size_t frameIndex) { 245 break;
189 // Initialize the frame if necessary. Some GIFs insert do-nothing frames, 246 case SkCodec::kIncompleteInput:
190 // in which case we never reach haveDecodedRow() before getting here. 247 if (frame.getStatus() == ImageFrame::FrameEmpty && index == 0) {
191 if (!initFrameBuffer(frameIndex)) 248 // We want to display the bit of the frame we have decoded only if it is
192 return false; // initFrameBuffer() has already called setFailed(). 249 // the first frame of an animation. That means we need to fill the rest
193 250 // of the image with transparent.
194 m_frameBufferCache[frameIndex].setStatus(ImageFrame::FrameComplete); 251 IntRect remainingRect = frame.originalFrameRect();
195 if (!m_currentBufferSawAlpha) 252 remainingRect.setHeight(remainingRect.height() - rowsDecoded);
196 correctAlphaWhenFrameBufferSawNoAlpha(frameIndex); 253 frame.zeroFillFrameRect(remainingRect);
197 254 }
198 return true; 255
199 } 256 frame.setStatus(ImageFrame::FramePartial);
200 257 frame.setPixelsChanged(true);
201 void GIFImageDecoder::clearFrameBuffer(size_t frameIndex) { 258 break;
202 if (m_reader && 259 default:
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(); 260 setFailed();
243 return; 261 return;
244 } 262 }
245 263
246 // If this returns false, we need more data to continue decoding. 264 correctAlphaWhenFrameBufferSawNoAlpha(index);
247 if (!postDecodeProcessing(*i)) 265
248 break; 266 if (!postDecodeProcessing(index)) {
249 } 267 return;
250 268 }
251 // It is also a fatal error if all data is received and we have decoded all 269 }
252 // frames available but the file is truncated. 270
253 if (index >= m_frameBufferCache.size() - 1 && isAllDataReceived() && 271 bool GIFImageDecoder::canReusePreviousFrameBuffer(size_t index) const {
254 m_reader && !m_reader->parseCompleted()) 272 DCHECK(index < m_frameBufferCache.size());
255 setFailed(); 273
256 } 274 // If the current frame and the next frame depend on the same frame, we cannot
257 275 // reuse the old frame. We must preserve it for the next frame.
258 void GIFImageDecoder::parse(GIFParseQuery query) { 276 //
259 if (failed()) 277 // However, if the current and next frame depend on different frames then we
260 return; 278 // know the current frame is the last one to use the frame it depends on. That
261 279 // means the current frame can reuse the previous frame buffer.
262 if (!m_reader) { 280 //
263 m_reader = WTF::makeUnique<GIFImageReader>(this); 281 // If we do not have information about the next frame yet, we cannot assume it
264 m_reader->setData(m_data); 282 // is safe to reuse the previous frame buffer.
265 } 283
266 284 if (index + 1 >= m_frameBufferCache.size()) {
267 if (!m_reader->parse(query)) 285 return false;
268 setFailed(); 286 }
269 } 287
270 288 const ImageFrame& frame = m_frameBufferCache[index];
271 void GIFImageDecoder::onInitFrameBuffer(size_t frameIndex) { 289 size_t requiredFrameIndex = frame.requiredPreviousFrameIndex();
272 m_currentBufferSawAlpha = false; 290
273 } 291 const ImageFrame& nextFrame = m_frameBufferCache[index + 1];
274 292 size_t nextRequiredFrameIndex = nextFrame.requiredPreviousFrameIndex();
275 bool GIFImageDecoder::canReusePreviousFrameBuffer(size_t frameIndex) const { 293
276 DCHECK(frameIndex < m_frameBufferCache.size()); 294 return requiredFrameIndex != nextRequiredFrameIndex;
277 return m_frameBufferCache[frameIndex].getDisposalMethod() !=
278 ImageFrame::DisposeOverwritePrevious;
279 } 295 }
280 296
281 } // namespace blink 297 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698