Index: third_party/WebKit/Source/platform/image-decoders/gif/GIFImageDecoder.cpp |
diff --git a/third_party/WebKit/Source/platform/image-decoders/gif/GIFImageDecoder.cpp b/third_party/WebKit/Source/platform/image-decoders/gif/GIFImageDecoder.cpp |
index 357917f67d3cff36f6b0a9e23a76c2d9e9ad86c2..10f12c530b5781574587db4b9a1cc8d8e02ee32a 100644 |
--- a/third_party/WebKit/Source/platform/image-decoders/gif/GIFImageDecoder.cpp |
+++ b/third_party/WebKit/Source/platform/image-decoders/gif/GIFImageDecoder.cpp |
@@ -25,10 +25,10 @@ |
#include "platform/image-decoders/gif/GIFImageDecoder.h" |
-#include "platform/image-decoders/gif/GIFImageReader.h" |
+#include <limits> |
+#include "third_party/skia/include/core/SkImageInfo.h" |
#include "wtf/NotFound.h" |
#include "wtf/PtrUtil.h" |
-#include <limits> |
namespace blink { |
@@ -36,246 +36,259 @@ GIFImageDecoder::GIFImageDecoder(AlphaOption alphaOption, |
const ColorBehavior& colorBehavior, |
size_t maxDecodedBytes) |
: ImageDecoder(alphaOption, colorBehavior, maxDecodedBytes), |
- m_repetitionCount(cAnimationLoopOnce) {} |
- |
-GIFImageDecoder::~GIFImageDecoder() {} |
+ m_codec(), |
+ m_segmentStream(nullptr) {} |
+ |
+GIFImageDecoder::~GIFImageDecoder() { |
+ if (!m_codec) { |
+ // if we did not create m_codec and thus did not pass ownership to it |
+ if (m_segmentStream) { |
+ delete m_segmentStream; |
+ } |
+ } |
+} |
void GIFImageDecoder::onSetData(SegmentReader* data) { |
- if (m_reader) |
- m_reader->setData(data); |
+ if (!data) { |
scroggo_chromium
2017/02/24 19:04:29
We may want to do something besides return. In the
cblume
2017/03/05 19:17:41
Done.
|
+ return; |
+ } |
+ |
+ if (!m_segmentStream) { |
+ m_segmentStream = new SegmentStream(); |
+ } |
+ |
+ if (!m_segmentStream) { |
scroggo_chromium
2017/02/24 19:04:29
The last block ensured that m_segmentStream is non
cblume
2017/03/05 19:17:41
Done.
|
+ return; |
+ } |
+ |
+ m_segmentStream->setReader(data, isAllDataReceived()); |
+ |
+ // If we don't have a SkCodec yet, create one from the stream |
+ if (!m_codec) { |
+ SkCodec* codec = SkCodec::NewFromStream(m_segmentStream); |
+ if (codec) { |
+ m_codec.reset(codec); |
+ } else { |
+ // m_segmentStream's ownership is passed. It is deleted if SkCodec |
+ // creation fails. In this case, release our reference so we can create a |
+ // new SegmentStream later. |
+ m_segmentStream = nullptr; |
+ return; |
+ } |
+ |
+ // SkCodec::NewFromStream will read enough of the image to get the image |
+ // size. |
+ SkImageInfo imageInfo = m_codec->getInfo(); |
+ setSize(imageInfo.width(), imageInfo.height()); |
+ } |
} |
int GIFImageDecoder::repetitionCount() const { |
+ if (!m_codec) { |
+ return 0; |
scroggo_chromium
2017/02/24 19:04:29
This happens to be cAnimationLoopOnce, but I think
cblume
2017/03/05 19:17:41
Done.
|
+ } |
+ |
// This value can arrive at any point in the image data stream. Most GIFs |
// in the wild declare it near the beginning of the file, so it usually is |
// set by the time we've decoded the size, but (depending on the GIF and the |
- // packets sent back by the webserver) not always. If the reader hasn't |
- // seen a loop count yet, it will return cLoopCountNotSeen, in which case we |
- // should default to looping once (the initial value for |
- // |m_repetitionCount|). |
- // |
- // There are some additional wrinkles here. First, ImageSource::clear() |
scroggo_chromium
2017/02/24 19:04:29
Just making a note to myself as I look over this c
|
- // may destroy the reader, making the result from the reader _less_ |
- // authoritative on future calls if the recreated reader hasn't seen the |
- // loop count. We don't need to special-case this because in this case the |
- // new reader will once again return cLoopCountNotSeen, and we won't |
- // overwrite the cached correct value. |
+ // packets sent back by the webserver) not always. |
// |
- // Second, a GIF might never set a loop count at all, in which case we |
- // should continue to treat it as a "loop once" animation. We don't need |
- // special code here either, because in this case we'll never change |
- // |m_repetitionCount| from its default value. |
- // |
- // Third, we use the same GIFImageReader for counting frames and we might |
- // see the loop count and then encounter a decoding error which happens |
- // later in the stream. It is also possible that no frames are in the |
- // stream. In these cases we should just loop once. |
- if (isAllDataReceived() && parseCompleted() && m_reader->imagesCount() == 1) |
scroggo_chromium
2017/02/24 19:04:30
Why was this case removed?
cblume
2017/03/05 19:17:41
Done.
|
- m_repetitionCount = cAnimationNone; |
- else if (failed() || (m_reader && (!m_reader->imagesCount()))) |
scroggo_chromium
2017/02/24 19:04:29
SkCodec will never report 0 images, but don't we s
cblume
2017/03/05 19:17:40
Done.
|
- m_repetitionCount = cAnimationLoopOnce; |
- else if (m_reader && m_reader->loopCount() != cLoopCountNotSeen) |
- m_repetitionCount = m_reader->loopCount(); |
- return m_repetitionCount; |
+ // SkCodec will parse forward in the file if the repetition count has not been |
+ // seen yet. |
+ |
+ int repetitionCount = m_codec->getRepetitionCount(); |
+ switch (repetitionCount) { |
+ case 0: |
+ return cAnimationNone; |
scroggo_chromium
2017/02/24 19:04:29
This should still return cAnimationLoopOnce (zero)
cblume
2017/03/05 19:17:41
Done.
|
+ case SkCodec::kRepetitionCountInfinite: |
+ return cAnimationLoopInfinite; |
+ default: |
+ return repetitionCount; |
+ } |
} |
bool GIFImageDecoder::frameIsCompleteAtIndex(size_t index) const { |
- return m_reader && (index < m_reader->imagesCount()) && |
- m_reader->frameContext(index)->isComplete(); |
-} |
+ if (!m_codec) { |
+ return false; |
+ } |
-float GIFImageDecoder::frameDurationAtIndex(size_t index) const { |
- return (m_reader && (index < m_reader->imagesCount()) && |
- m_reader->frameContext(index)->isHeaderDefined()) |
- ? m_reader->frameContext(index)->delayTime() |
- : 0; |
-} |
+ std::vector<SkCodec::FrameInfo> frameInfos = m_codec->getFrameInfo(); |
+ if (frameInfos.size() <= index) { |
+ return false; |
+ } |
-bool GIFImageDecoder::setFailed() { |
- m_reader.reset(); |
- return ImageDecoder::setFailed(); |
+ return frameInfos[index].fFullyReceived; |
} |
-bool GIFImageDecoder::haveDecodedRow(size_t frameIndex, |
- GIFRow::const_iterator rowBegin, |
- size_t width, |
- size_t rowNumber, |
- unsigned repeatCount, |
- bool writeTransparentPixels) { |
- const GIFFrameContext* frameContext = m_reader->frameContext(frameIndex); |
- // The pixel data and coordinates supplied to us are relative to the frame's |
- // origin within the entire image size, i.e. |
- // (frameContext->xOffset, frameContext->yOffset). There is no guarantee |
- // that width == (size().width() - frameContext->xOffset), so |
- // we must ensure we don't run off the end of either the source data or the |
- // row's X-coordinates. |
- const int xBegin = frameContext->xOffset(); |
- const int yBegin = frameContext->yOffset() + rowNumber; |
- const int xEnd = std::min(static_cast<int>(frameContext->xOffset() + width), |
- size().width()); |
- const int yEnd = std::min( |
- static_cast<int>(frameContext->yOffset() + rowNumber + repeatCount), |
- size().height()); |
- if (!width || (xBegin < 0) || (yBegin < 0) || (xEnd <= xBegin) || |
- (yEnd <= yBegin)) |
- return true; |
- |
- const GIFColorMap::Table& colorTable = |
- frameContext->localColorMap().isDefined() |
- ? frameContext->localColorMap().getTable() |
- : m_reader->globalColorMap().getTable(); |
- |
- if (colorTable.isEmpty()) |
- return true; |
- |
- GIFColorMap::Table::const_iterator colorTableIter = colorTable.begin(); |
- |
- // Initialize the frame if necessary. |
- ImageFrame& buffer = m_frameBufferCache[frameIndex]; |
- if (!initFrameBuffer(frameIndex)) |
- return false; |
- |
- const size_t transparentPixel = frameContext->transparentPixel(); |
- GIFRow::const_iterator rowEnd = rowBegin + (xEnd - xBegin); |
- ImageFrame::PixelData* currentAddress = buffer.getAddr(xBegin, yBegin); |
- |
- // We may or may not need to write transparent pixels to the buffer. |
- // If we're compositing against a previous image, it's wrong, and if |
- // we're writing atop a cleared, fully transparent buffer, it's |
- // unnecessary; but if we're decoding an interlaced gif and |
- // displaying it "Haeberli"-style, we must write these for passes |
- // beyond the first, or the initial passes will "show through" the |
- // later ones. |
- // |
- // The loops below are almost identical. One writes a transparent pixel |
- // and one doesn't based on the value of |writeTransparentPixels|. |
- // The condition check is taken out of the loop to enhance performance. |
- // This optimization reduces decoding time by about 15% for a 3MB image. |
- if (writeTransparentPixels) { |
- for (; rowBegin != rowEnd; ++rowBegin, ++currentAddress) { |
- const size_t sourceValue = *rowBegin; |
- if ((sourceValue != transparentPixel) && |
- (sourceValue < colorTable.size())) { |
- *currentAddress = colorTableIter[sourceValue]; |
- } else { |
- *currentAddress = 0; |
- m_currentBufferSawAlpha = true; |
- } |
- } |
- } else { |
- for (; rowBegin != rowEnd; ++rowBegin, ++currentAddress) { |
- const size_t sourceValue = *rowBegin; |
- if ((sourceValue != transparentPixel) && |
- (sourceValue < colorTable.size())) |
- *currentAddress = colorTableIter[sourceValue]; |
- else |
- m_currentBufferSawAlpha = true; |
- } |
+float GIFImageDecoder::frameDurationAtIndex(size_t index) const { |
+ if (!m_codec) { |
+ return 0; |
} |
- // Tell the frame to copy the row data if need be. |
- if (repeatCount > 1) |
- buffer.copyRowNTimes(xBegin, xEnd, yBegin, yEnd); |
+ std::vector<SkCodec::FrameInfo> frameInfos = m_codec->getFrameInfo(); |
+ if (frameInfos.size() <= index) { |
+ return 0; |
+ } |
- buffer.setPixelsChanged(true); |
- return true; |
+ return frameInfos[index].fDuration; |
} |
-bool GIFImageDecoder::parseCompleted() const { |
- return m_reader && m_reader->parseCompleted(); |
+size_t GIFImageDecoder::decodeFrameCount() { |
+ if (!m_codec) { |
+ return 0; |
+ } |
+ |
+ std::vector<SkCodec::FrameInfo> frameInfos = m_codec->getFrameInfo(); |
+ return frameInfos.size(); |
} |
-bool GIFImageDecoder::frameComplete(size_t frameIndex) { |
- // Initialize the frame if necessary. Some GIFs insert do-nothing frames, |
- // in which case we never reach haveDecodedRow() before getting here. |
- if (!initFrameBuffer(frameIndex)) |
- return false; // initFrameBuffer() has already called setFailed(). |
+void GIFImageDecoder::decode(size_t index) { |
+ if (failed()) |
+ return; |
- m_frameBufferCache[frameIndex].setStatus(ImageFrame::FrameComplete); |
- if (!m_currentBufferSawAlpha) |
- correctAlphaWhenFrameBufferSawNoAlpha(frameIndex); |
+ if (!m_codec) |
+ return; |
- return true; |
-} |
+ if (m_frameBufferCache.size() <= index) { |
+ // It is a fatal error if all data is received and we have decoded all |
+ // frames available but the file is truncated. |
+ if (isAllDataReceived()) { |
+ setFailed(); |
+ } |
-void GIFImageDecoder::clearFrameBuffer(size_t frameIndex) { |
- if (m_reader && |
- m_frameBufferCache[frameIndex].getStatus() == ImageFrame::FramePartial) { |
- // Reset the state of the partial frame in the reader so that the frame |
- // can be decoded again when requested. |
- m_reader->clearDecodeState(frameIndex); |
+ return; |
} |
- ImageDecoder::clearFrameBuffer(frameIndex); |
-} |
-size_t GIFImageDecoder::decodeFrameCount() { |
- parse(GIFFrameCountQuery); |
- // If decoding fails, |m_reader| will have been destroyed. Instead of |
- // returning 0 in this case, return the existing number of frames. This way |
- // if we get halfway through the image before decoding fails, we won't |
- // suddenly start reporting that the image has zero frames. |
- return failed() ? m_frameBufferCache.size() : m_reader->imagesCount(); |
-} |
+ updateAggressivePurging(index); |
-void GIFImageDecoder::initializeNewFrame(size_t index) { |
- ImageFrame* buffer = &m_frameBufferCache[index]; |
- const GIFFrameContext* frameContext = m_reader->frameContext(index); |
- buffer->setOriginalFrameRect( |
- intersection(frameContext->frameRect(), IntRect(IntPoint(), size()))); |
- buffer->setDuration(frameContext->delayTime()); |
- buffer->setDisposalMethod(frameContext->getDisposalMethod()); |
- buffer->setRequiredPreviousFrameIndex( |
- findRequiredPreviousFrame(index, false)); |
-} |
+ SkImageInfo imageInfo = m_codec->getInfo().makeColorType(kN32_SkColorType); |
-void GIFImageDecoder::decode(size_t index) { |
- parse(GIFFrameCountQuery); |
+ ImageFrame& frame = m_frameBufferCache[index]; |
- if (failed()) |
- return; |
+ std::vector<SkCodec::FrameInfo> frameInfos = m_codec->getFrameInfo(); |
+ size_t requiredPreviousFrameIndex = frameInfos[index].fRequiredFrame; |
+ if (requiredPreviousFrameIndex == SkCodec::kNone) { |
+ requiredPreviousFrameIndex = WTF::kNotFound; |
+ } |
+ frame.setRequiredPreviousFrameIndex(requiredPreviousFrameIndex); |
- updateAggressivePurging(index); |
+ SkCodec::Options options; |
+ options.fFrameIndex = index; |
+ options.fHasPriorFrame = false; |
+ if (requiredPreviousFrameIndex != WTF::kNotFound) { |
+ options.fHasPriorFrame = true; |
scroggo_chromium
2017/02/24 19:04:29
Doesn't this need to check whether we actually hav
cblume
2017/03/05 19:17:41
I think I see what you are saying.
On line 198 bel
|
+ } |
- Vector<size_t> framesToDecode = findFramesToDecode(index); |
- for (auto i = framesToDecode.rbegin(); i != framesToDecode.rend(); ++i) { |
- if (!m_reader->decode(*i)) { |
- setFailed(); |
- return; |
+ if (frame.getStatus() == ImageFrame::FrameEmpty) { |
+ IntSize frameSize = size(); |
+ frame.setOriginalFrameRect(IntRect(IntPoint(), frameSize)); |
scroggo_chromium
2017/02/24 19:04:30
Add a comment that this is incorrect?
cblume
2017/03/05 19:17:41
Perhaps I misunderstood what this is supposed to r
scroggo_chromium
2017/03/06 16:27:31
As I understand it, it represents the portion of t
cblume
2017/03/07 01:18:10
Ohhh okay, I understand what you mean.
You mean th
|
+ frame.setDuration(frameInfos[index].fDuration); |
+ // The disposal method is not required any more, but is left in place |
+ // for the other image decoders that do not yet rely on SkCodec. |
+ // For now, fill it with DisposeKeep. |
+ frame.setDisposalMethod(ImageFrame::DisposeKeep); |
+ if (requiredPreviousFrameIndex == WTF::kNotFound) { |
+ frame.setSizeAndColorSpace(frameSize.width(), frameSize.height(), |
scroggo_chromium
2017/02/24 19:04:29
This seems a little misleading. If frameSize corre
cblume
2017/03/05 19:17:41
I'm similarly unsure what you mean here.
The frame
scroggo_chromium
2017/03/06 16:27:31
When I hear "frameSize", I think "the size of the
cblume
2017/03/07 01:18:10
I understand now. And I agree with you. Fixed.
|
+ colorSpaceForSkImages()); |
+ } else { |
+ ImageFrame& requiredPreviousFrame = |
+ m_frameBufferCache[requiredPreviousFrameIndex]; |
+ |
+ if (requiredPreviousFrame.getStatus() != ImageFrame::FrameComplete) |
+ decode(requiredPreviousFrameIndex); |
+ |
+ // We try to reuse |requiredPreviousFrame| as starting state to avoid |
+ // copying. If canReusePreviousFrameBuffer returns false, we must copy |
+ // the data since |requiredPreviousFrame| is necessary to decode this |
+ // or later frames. In that case copy the data instead. |
+ /* |
+ if ((!canReusePreviousFrameBuffer(index) || |
+ !frame.takeBitmapDataIfWritable(&requiredPreviousFrame)) && |
+ !frame.copyBitmapData(requiredPreviousFrame)) { |
+ */ |
+ if (!frame.copyBitmapData(requiredPreviousFrame)) { |
+ setFailed(); |
+ return; |
+ } |
} |
- // If this returns false, we need more data to continue decoding. |
- if (!postDecodeProcessing(*i)) |
+ SkCodec::Result startIncrementalDecodeResult = |
+ m_codec->startIncrementalDecode(imageInfo, frame.bitmap().getPixels(), |
+ frame.bitmap().rowBytes(), &options, |
+ nullptr, nullptr); |
+ switch (startIncrementalDecodeResult) { |
+ case SkCodec::kSuccess: |
+ break; |
+ case SkCodec::kIncompleteInput: |
+ return; |
+ default: |
+ setFailed(); |
+ return; |
+ } |
scroggo_chromium
2017/02/24 19:04:29
Should this set the status to FramePartial, so we
cblume
2017/03/05 19:17:41
Done.
|
+ } |
+ |
+ int rowsDecoded = 0; |
+ SkCodec::Result incrementalDecodeResult = |
+ m_codec->incrementalDecode(&rowsDecoded); |
+ switch (incrementalDecodeResult) { |
+ case SkCodec::kSuccess: |
+ correctAlphaWhenFrameBufferSawNoAlpha(index); |
scroggo_chromium
2017/02/24 19:04:30
This should only be called if there was no alpha.
cblume
2017/03/05 19:17:40
Oh, got ya.
So presumably this is now done within
cblume
2017/03/06 03:21:36
I have tried removing this, but it causes the GIFI
msarett1
2017/03/06 13:54:00
radient.gif (the image used in GIFImageDecoderTest
scroggo_chromium
2017/03/06 16:27:31
Well, you're "correcting it" regardless of whether
cblume
2017/03/07 01:18:10
I think calling this method at all is a problem.
|
+ frame.setPixelsChanged(true); |
+ frame.setStatus(ImageFrame::FrameComplete); |
+ break; |
+ case SkCodec::kIncompleteInput: |
+ if (isAllDataReceived()) { |
+ setFailed(); |
+ return; |
+ } |
+ |
+ if (frame.getStatus() == ImageFrame::FrameEmpty && index == 0) { |
+ // We want to display the bit of the frame we have decoded only if it is |
scroggo_chromium
2017/02/24 19:04:30
SkCodec does not report a frame beyond the first u
cblume
2017/03/05 19:17:41
Done.
|
+ // the first frame of an animation. That means we need to fill the rest |
scroggo_chromium
2017/02/24 19:04:29
Everything already got zeroed by your call to setS
cblume
2017/03/05 19:17:41
Done.
|
+ // of the image with transparent. |
+ IntRect remainingRect = frame.originalFrameRect(); |
+ remainingRect.setY(rowsDecoded); |
+ remainingRect.setHeight(remainingRect.height() - rowsDecoded); |
+ frame.zeroFillFrameRect(remainingRect); |
+ |
+ frame.setPixelsChanged(true); |
+ frame.setStatus(ImageFrame::FramePartial); |
+ } |
break; |
+ default: |
+ setFailed(); |
+ return; |
} |
- // It is also a fatal error if all data is received and we have decoded all |
- // frames available but the file is truncated. |
- if (index >= m_frameBufferCache.size() - 1 && isAllDataReceived() && |
- m_reader && !m_reader->parseCompleted()) |
- setFailed(); |
+ if (frame.getStatus() == ImageFrame::FrameComplete) { |
+ if (!postDecodeProcessing(index)) |
+ return; |
+ } |
} |
-void GIFImageDecoder::parse(GIFParseQuery query) { |
- if (failed()) |
- return; |
+bool GIFImageDecoder::canReusePreviousFrameBuffer(size_t index) const { |
+ DCHECK(index < m_frameBufferCache.size()); |
- if (!m_reader) { |
- m_reader = WTF::makeUnique<GIFImageReader>(this); |
- m_reader->setData(m_data); |
- } |
+ // If the current frame and the next frame depend on the same frame, we cannot |
+ // reuse the old frame. We must preserve it for the next frame. |
+ // |
+ // However, if the current and next frame depend on different frames then we |
+ // know the current frame is the last one to use the frame it depends on. That |
+ // means the current frame can reuse the previous frame buffer. |
+ // |
+ // If we do not have information about the next frame yet, we cannot assume it |
+ // is safe to reuse the previous frame buffer. |
- if (!m_reader->parse(query)) |
- setFailed(); |
-} |
+ if (index + 1 >= m_frameBufferCache.size()) |
+ return false; |
-void GIFImageDecoder::onInitFrameBuffer(size_t frameIndex) { |
- m_currentBufferSawAlpha = false; |
-} |
+ const ImageFrame& frame = m_frameBufferCache[index]; |
+ size_t requiredFrameIndex = frame.requiredPreviousFrameIndex(); |
+ |
+ const ImageFrame& nextFrame = m_frameBufferCache[index + 1]; |
+ size_t nextRequiredFrameIndex = nextFrame.requiredPreviousFrameIndex(); |
-bool GIFImageDecoder::canReusePreviousFrameBuffer(size_t frameIndex) const { |
- DCHECK(frameIndex < m_frameBufferCache.size()); |
- return m_frameBufferCache[frameIndex].getDisposalMethod() != |
- ImageFrame::DisposeOverwritePrevious; |
+ return requiredFrameIndex != nextRequiredFrameIndex; |
} |
} // namespace blink |