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

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

Issue 2565323003: Move gif image decoder to SkCodec (Closed)
Patch Set: Setting has alpha on a partial decode. Created 3 years, 8 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 side-by-side diff with in-line comments
Download patch
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 25a00e19e67b15afea346b667c28500d2a13b27d..aba962f7479162295dae40c5ee94a7f4f9767330 100644
--- a/third_party/WebKit/Source/platform/image-decoders/gif/GIFImageDecoder.cpp
+++ b/third_party/WebKit/Source/platform/image-decoders/gif/GIFImageDecoder.cpp
@@ -26,9 +26,9 @@
#include "platform/image-decoders/gif/GIFImageDecoder.h"
#include <limits>
-#include "platform/image-decoders/gif/GIFImageReader.h"
#include "platform/wtf/NotFound.h"
#include "platform/wtf/PtrUtil.h"
+#include "third_party/skia/include/core/SkImageInfo.h"
namespace blink {
@@ -36,247 +36,252 @@ GIFImageDecoder::GIFImageDecoder(AlphaOption alpha_option,
const ColorBehavior& color_behavior,
size_t max_decoded_bytes)
: ImageDecoder(alpha_option, color_behavior, max_decoded_bytes),
- repetition_count_(kCAnimationLoopOnce) {}
-
-GIFImageDecoder::~GIFImageDecoder() {}
+ codec_(),
+ segment_stream_(nullptr),
+ frame_infos_() {}
+
+GIFImageDecoder::~GIFImageDecoder() {
+ if (!codec_) {
+ // 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.
+ if (segment_stream_)
+ delete segment_stream_;
+ }
+}
void GIFImageDecoder::OnSetData(SegmentReader* data) {
- if (reader_)
- reader_->setData(data);
+ if (!data) {
+ if (segment_stream_)
+ segment_stream_->SetReader(nullptr, false);
+ return;
+ }
+
+ if (!segment_stream_)
+ segment_stream_ = new SegmentStream();
+
+ segment_stream_->SetReader(data, IsAllDataReceived());
+
+ // If we don't have a SkCodec yet, create one from the stream
+ if (!codec_) {
+ 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.
+ if (codec) {
+ codec_.reset(codec);
+ } else {
+ // segment_stream_'s ownership is passed. It is deleted if SkCodec
+ // 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.
+ // new SegmentStream later.
+ segment_stream_ = nullptr;
+ return;
+ }
+
+ // SkCodec::NewFromStream will read enough of the image to get the image
+ // size.
+ SkImageInfo image_info = codec_->getInfo();
+ SetSize(image_info.width(), image_info.height());
+ }
}
int GIFImageDecoder::RepetitionCount() const {
+ if (!codec_)
+ return kCAnimationLoopOnce;
+
// 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()
- // 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() && reader_->imagesCount() == 1)
- repetition_count_ = kCAnimationNone;
- else if (Failed() || (reader_ && (!reader_->imagesCount())))
- repetition_count_ = kCAnimationLoopOnce;
- else if (reader_ && reader_->loopCount() != cLoopCountNotSeen)
- repetition_count_ = reader_->loopCount();
- return repetition_count_;
-}
+ // SkCodec will parse forward in the file if the repetition count has not been
+ // seen yet.
-bool GIFImageDecoder::FrameIsCompleteAtIndex(size_t index) const {
- return reader_ && (index < reader_->imagesCount()) &&
- reader_->frameContext(index)->isComplete();
-}
-
-float GIFImageDecoder::FrameDurationAtIndex(size_t index) const {
- return (reader_ && (index < reader_->imagesCount()) &&
- reader_->frameContext(index)->isHeaderDefined())
- ? reader_->frameContext(index)->delayTime()
- : 0;
-}
-
-bool GIFImageDecoder::SetFailed() {
- reader_.reset();
- return ImageDecoder::SetFailed();
-}
-
-bool GIFImageDecoder::HaveDecodedRow(size_t frame_index,
- GIFRow::const_iterator row_begin,
- size_t width,
- size_t row_number,
- unsigned repeat_count,
- bool write_transparent_pixels) {
- const GIFFrameContext* frame_context = reader_->frameContext(frame_index);
- // 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 x_begin = frame_context->xOffset();
- const int y_begin = frame_context->yOffset() + row_number;
- const int x_end = std::min(static_cast<int>(frame_context->xOffset() + width),
- Size().Width());
- const int y_end = std::min(
- static_cast<int>(frame_context->yOffset() + row_number + repeat_count),
- Size().Height());
- if (!width || (x_begin < 0) || (y_begin < 0) || (x_end <= x_begin) ||
- (y_end <= y_begin))
- return true;
-
- const GIFColorMap::Table& color_table =
- frame_context->localColorMap().isDefined()
- ? frame_context->localColorMap().getTable()
- : reader_->globalColorMap().getTable();
-
- if (color_table.IsEmpty())
- return true;
-
- GIFColorMap::Table::const_iterator color_table_iter = color_table.begin();
-
- // Initialize the frame if necessary.
- ImageFrame& buffer = frame_buffer_cache_[frame_index];
- if (!InitFrameBuffer(frame_index))
- return false;
-
- const size_t transparent_pixel = frame_context->transparentPixel();
- GIFRow::const_iterator row_end = row_begin + (x_end - x_begin);
- ImageFrame::PixelData* current_address = buffer.GetAddr(x_begin, y_begin);
-
- // 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 (write_transparent_pixels) {
- for (; row_begin != row_end; ++row_begin, ++current_address) {
- const size_t source_value = *row_begin;
- if ((source_value != transparent_pixel) &&
- (source_value < color_table.size())) {
- *current_address = color_table_iter[source_value];
- } else {
- *current_address = 0;
- current_buffer_saw_alpha_ = true;
- }
- }
- } else {
- for (; row_begin != row_end; ++row_begin, ++current_address) {
- const size_t source_value = *row_begin;
- if ((source_value != transparent_pixel) &&
- (source_value < color_table.size()))
- *current_address = color_table_iter[source_value];
- else
- current_buffer_saw_alpha_ = true;
- }
+ 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
+ return kCAnimationNone;
+ if (Failed())
+ return kCAnimationLoopOnce;
+
+ int repetition_count = codec_->getRepetitionCount();
+ switch (repetition_count) {
+ case 0:
+ return kCAnimationLoopOnce;
+ case SkCodec::kRepetitionCountInfinite:
+ return kCAnimationLoopInfinite;
+ default:
+ return repetition_count;
}
-
- // Tell the frame to copy the row data if need be.
- if (repeat_count > 1)
- buffer.CopyRowNTimes(x_begin, x_end, y_begin, y_end);
-
- buffer.SetPixelsChanged(true);
- return true;
}
-bool GIFImageDecoder::ParseCompleted() const {
- return reader_ && reader_->parseCompleted();
-}
-
-bool GIFImageDecoder::FrameComplete(size_t frame_index) {
- // Initialize the frame if necessary. Some GIFs insert do-nothing frames,
- // in which case we never reach haveDecodedRow() before getting here.
- if (!InitFrameBuffer(frame_index))
- return SetFailed();
-
- if (!current_buffer_saw_alpha_)
- CorrectAlphaWhenFrameBufferSawNoAlpha(frame_index);
+bool GIFImageDecoder::FrameIsCompleteAtIndex(size_t index) const {
+ if (!codec_)
+ return false;
- frame_buffer_cache_[frame_index].SetStatus(ImageFrame::kFrameComplete);
+ if (frame_infos_.size() <= index)
+ return false;
- return true;
+ return frame_infos_[index].fFullyReceived;
}
-void GIFImageDecoder::ClearFrameBuffer(size_t frame_index) {
- if (reader_ && frame_buffer_cache_[frame_index].GetStatus() ==
- ImageFrame::kFramePartial) {
- // Reset the state of the partial frame in the reader so that the frame
- // can be decoded again when requested.
- reader_->clearDecodeState(frame_index);
- }
- ImageDecoder::ClearFrameBuffer(frame_index);
+float GIFImageDecoder::FrameDurationAtIndex(size_t index) const {
+ if (index < frame_buffer_cache_.size())
+ return frame_buffer_cache_[index].Duration();
+ return 0;
}
size_t GIFImageDecoder::DecodeFrameCount() {
- Parse(kGIFFrameCountQuery);
- // 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() ? frame_buffer_cache_.size() : reader_->imagesCount();
+ if (!codec_)
+ return 0;
+
+ 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.
+ frame_infos_ = codec_->getFrameInfo();
+ return frame_infos_.size();
}
void GIFImageDecoder::InitializeNewFrame(size_t index) {
- ImageFrame* buffer = &frame_buffer_cache_[index];
- const GIFFrameContext* frame_context = reader_->frameContext(index);
- buffer->SetOriginalFrameRect(
- Intersection(frame_context->frameRect(), IntRect(IntPoint(), Size())));
- buffer->SetDuration(frame_context->delayTime());
- buffer->SetDisposalMethod(frame_context->getDisposalMethod());
- buffer->SetRequiredPreviousFrameIndex(
- FindRequiredPreviousFrame(index, false));
+ DCHECK(codec_);
+
+ ImageFrame& frame = frame_buffer_cache_[index];
+ // SkCodec does not inform us if only a portion of the image was updated
+ // in the current frame. Because of this, rather than correctly filling in
+ // the frame rect, we set the frame rect to be the image's full size.
+ IntSize full_image_size = Size();
+ frame.SetOriginalFrameRect(IntRect(IntPoint(), full_image_size));
+ frame.SetDuration(frame_infos_[index].fDuration);
+ 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.
+ frame_infos_[index].fAlphaType == kUnpremul_SkAlphaType);
+ size_t required_previous_frame_index = frame_infos_[index].fRequiredFrame;
+ if (required_previous_frame_index == SkCodec::kNone)
+ required_previous_frame_index = WTF::kNotFound;
+ frame.SetRequiredPreviousFrameIndex(required_previous_frame_index);
+ // 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::kDisposeKeep);
}
void GIFImageDecoder::Decode(size_t index) {
- Parse(kGIFFrameCountQuery);
-
if (Failed())
return;
- UpdateAggressivePurging(index);
+ if (!codec_)
+ return;
- Vector<size_t> frames_to_decode = FindFramesToDecode(index);
- for (auto i = frames_to_decode.rbegin(); i != frames_to_decode.rend(); ++i) {
- if (!reader_->decode(*i)) {
+ 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.
+ return;
+
+ if (frame_buffer_cache_.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();
- return;
+
+ return;
+ }
+
+ UpdateAggressivePurging(index);
+ SkImageInfo image_info = codec_->getInfo().makeColorType(kN32_SkColorType);
+
+ SkCodec::Options options;
+ options.fFrameIndex = index;
+ options.fHasPriorFrame = false;
+ options.fZeroInitialized = SkCodec::kYes_ZeroInitialized;
+
+ ImageFrame& frame = frame_buffer_cache_[index];
+ if (frame.GetStatus() == ImageFrame::kFrameEmpty) {
+ size_t required_previous_frame_index = frame.RequiredPreviousFrameIndex();
+ if (required_previous_frame_index == WTF::kNotFound) {
+ frame.AllocatePixelData(Size().Width(), Size().Height(),
+ ColorSpaceForSkImages());
+ 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
+ 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
+ frame_infos_[index].fAlphaType ==
+ kUnpremul_SkAlphaType);
+ } else {
+ ImageFrame& required_previous_frame =
+ frame_buffer_cache_[required_previous_frame_index];
+
+ if (required_previous_frame.GetStatus() != ImageFrame::kFrameComplete)
+ Decode(required_previous_frame_index);
+
+ // We try to reuse |required_previous_frame| as starting state to avoid
+ // copying. If CanReusePreviousFrameBuffer returns false, we must copy
+ // the data since |required_previous_frame| is necessary to decode this
+ // or later frames. In that case copy the data instead.
+ if ((!CanReusePreviousFrameBuffer(index) ||
+ !frame.TakeBitmapDataIfWritable(&required_previous_frame)) &&
+ !frame.CopyBitmapData(required_previous_frame)) {
+ SetFailed();
+ return;
+ }
+ options.fHasPriorFrame = true;
}
+ SkCodec::Result start_incremental_decode_result =
+ codec_->startIncrementalDecode(image_info, frame.Bitmap().getPixels(),
+ frame.Bitmap().rowBytes(), &options,
+ nullptr, nullptr);
+ switch (start_incremental_decode_result) {
+ case SkCodec::kSuccess:
+ break;
+ case SkCodec::kIncompleteInput:
+ return;
+ default:
+ SetFailed();
+ return;
+ }
+ frame.SetStatus(ImageFrame::kFramePartial);
+ }
+ int rows_decoded = 0;
+ SkCodec::Result incremental_decode_result =
+ codec_->incrementalDecode(&rows_decoded);
+ switch (incremental_decode_result) {
+ case SkCodec::kSuccess:
+ 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
+ frame_infos_[index].fAlphaType ==
+ kUnpremul_SkAlphaType);
+ frame.SetPixelsChanged(true);
+ frame.SetStatus(ImageFrame::kFrameComplete);
+ PostDecodeProcessing(index);
+ break;
+ case SkCodec::kIncompleteInput:
+ if (FrameIsCompleteAtIndex(index) || IsAllDataReceived()) {
+ SetFailed();
+ return;
+ }
+ {
+ IntRect remaining_rect = frame.OriginalFrameRect();
+ remaining_rect.SetY(rows_decoded);
+ remaining_rect.SetHeight(remaining_rect.Height() - rows_decoded);
+ frame.ZeroFillFrameRect(remaining_rect);
+ frame.SetHasAlpha(true);
+ }
- // If this returns false, we need more data to continue decoding.
- if (!PostDecodeProcessing(*i))
+ frame.SetPixelsChanged(true);
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 >= frame_buffer_cache_.size() - 1 && IsAllDataReceived() &&
- reader_ && !reader_->parseCompleted())
- SetFailed();
}
-void GIFImageDecoder::Parse(GIFParseQuery query) {
- if (Failed())
- return;
+bool GIFImageDecoder::CanReusePreviousFrameBuffer(size_t index) const {
+ DCHECK(index < frame_buffer_cache_.size());
- if (!reader_) {
- reader_ = WTF::MakeUnique<GIFImageReader>(this);
- reader_->setData(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
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
+ // 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 (!reader_->parse(query))
- SetFailed();
-}
+ if (index + 1 >= frame_buffer_cache_.size())
+ return false;
-void GIFImageDecoder::OnInitFrameBuffer(size_t frame_index) {
- current_buffer_saw_alpha_ = false;
-}
+ const ImageFrame& frame = frame_buffer_cache_[index];
+ size_t required_frame_index = frame.RequiredPreviousFrameIndex();
+
+ const ImageFrame& next_frame = frame_buffer_cache_[index + 1];
+ size_t next_required_frame_index = next_frame.RequiredPreviousFrameIndex();
-bool GIFImageDecoder::CanReusePreviousFrameBuffer(size_t frame_index) const {
- DCHECK(frame_index < frame_buffer_cache_.size());
- return frame_buffer_cache_[frame_index].GetDisposalMethod() !=
- ImageFrame::kDisposeOverwritePrevious;
+ return required_frame_index != next_required_frame_index;
}
} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698