Chromium Code Reviews| Index: third_party/WebKit/Source/platform/image-decoders/SegmentStream.cpp | 
| diff --git a/third_party/WebKit/Source/platform/image-decoders/SegmentStream.cpp b/third_party/WebKit/Source/platform/image-decoders/SegmentStream.cpp | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..6e9bcdd0e984035c3588f471119ab1206ba7f557 | 
| --- /dev/null | 
| +++ b/third_party/WebKit/Source/platform/image-decoders/SegmentStream.cpp | 
| @@ -0,0 +1,110 @@ | 
| +// Copyright (c) 2017 The Chromium Authors. All rights reserved. | 
| +// Use of this source code is governed by a BSD-style license that can be | 
| +// found in the LICENSE file. | 
| + | 
| +#include "platform/image-decoders/SegmentStream.h" | 
| + | 
| +namespace blink { | 
| + | 
| +SegmentStream::SegmentStream() | 
| + : reader_(), | 
| + position_(0), | 
| 
 
vmpstr
2017/07/17 21:28:40
nit: you can initialize these in the header and =
 
cblume
2017/07/18 10:23:48
Done.
 
 | 
| + has_read_all_contents_(true), | 
| + is_cleared_(true) {} | 
| + | 
| +SegmentStream::SegmentStream(SegmentStream&& rhs) | 
| 
 
vmpstr
2017/07/17 21:28:40
nit: = default;
 
cblume
2017/07/18 10:23:48
No can do on this one.
This class derives from Sk
 
cblume
2017/07/18 10:45:57
*However, SkStream does not provide any *EXPLICIT*
 
vmpstr
2017/07/18 18:02:48
Acknowledged. I think there is no implicitly gener
 
cblume
2017/07/19 23:27:04
I made a Skia patch for it: https://skia-review.go
 
 | 
| + : reader_(rhs.reader_), | 
| + position_(rhs.position_), | 
| + has_read_all_contents_(rhs.has_read_all_contents_), | 
| + is_cleared_(rhs.is_cleared_) {} | 
| + | 
| +SegmentStream& SegmentStream::operator=(SegmentStream&& rhs) { | 
| + reader_ = rhs.reader_; | 
| 
 
vmpstr
2017/07/17 21:28:40
nit: reader_ = std::move(rhs.reader_);
 
cblume
2017/07/18 10:23:48
Done.
 
 | 
| + position_ = rhs.position_; | 
| + has_read_all_contents_ = rhs.has_read_all_contents_; | 
| + is_cleared_ = rhs.is_cleared_; | 
| + | 
| + return *this; | 
| +} | 
| + | 
| +void SegmentStream::SetReader(WTF::PassRefPtr<SegmentReader> reader) { | 
| + reader_ = std::move(reader); | 
| + SetPositionState(position_); | 
| +} | 
| + | 
| +size_t SegmentStream::read(void* buffer, size_t size) { | 
| + if (is_cleared_) | 
| + return 0; | 
| + | 
| + size = std::min(size, reader_->size() - position_); | 
| + | 
| + size_t bytes_advanced = 0; | 
| + if (!buffer) { // skipping, not reading | 
| + bytes_advanced = size; | 
| + } else { | 
| + bytes_advanced = peek(buffer, size); | 
| + } | 
| + | 
| + SetPositionState(position_ + bytes_advanced); | 
| + | 
| + return bytes_advanced; | 
| +} | 
| + | 
| +size_t SegmentStream::peek(void* buffer, size_t size) const { | 
| + if (is_cleared_) | 
| + return 0; | 
| + | 
| + size = std::min(size, reader_->size() - position_); | 
| + | 
| + size_t peek_position = position_; | 
| 
 
vmpstr
2017/07/17 21:28:40
nit: I don't think you really need this, GetSomeDa
 
cblume
2017/07/18 10:23:48
Done.
 
 | 
| + size_t total_bytes_peeked = 0; | 
| + char* buffer_as_char_ptr = reinterpret_cast<char*>(buffer); | 
| + while (size) { | 
| + const char* segment = nullptr; | 
| + size_t bytes_peeked = reader_->GetSomeData(segment, peek_position); | 
| + if (!bytes_peeked) | 
| + break; | 
| + if (bytes_peeked > size) | 
| + bytes_peeked = size; | 
| + | 
| + memcpy(buffer_as_char_ptr, segment, bytes_peeked); | 
| + buffer_as_char_ptr += bytes_peeked; | 
| + size -= bytes_peeked; | 
| + total_bytes_peeked += bytes_peeked; | 
| + peek_position += bytes_peeked; | 
| + } | 
| + | 
| + return total_bytes_peeked; | 
| +} | 
| + | 
| +bool SegmentStream::rewind() { | 
| + SetPositionState(0); | 
| + return true; | 
| +} | 
| + | 
| +bool SegmentStream::seek(size_t position) { | 
| + SetPositionState(position); | 
| + return true; | 
| +} | 
| + | 
| +bool SegmentStream::move(long offset) { | 
| + long absolute_position = position_ + offset; | 
| + | 
| + absolute_position = std::max(absolute_position, 0l); | 
| 
 
vmpstr
2017/07/17 21:28:40
Should this just be a DCHECK absolute_position > 0
 
cblume
2017/07/18 10:23:48
It has been a while. I'll change this to a DCHECK
 
scroggo_chromium
2017/07/18 17:52:41
SkGifCodec won't call move at all (it uses seek in
 
cblume
2017/07/19 23:27:04
Do you happen to know about the other decoders?
Th
 
scroggo_chromium
2017/07/20 13:59:52
Oh, oops, SkGifCodec does use it. nvm.
 
 | 
| + SetPositionState(absolute_position); | 
| + return true; | 
| +} | 
| + | 
| +void SegmentStream::SetPositionState(size_t new_position) { | 
| + position_ = new_position; | 
| + if (reader_) { | 
| + const size_t reader_size = reader_->size(); | 
| + has_read_all_contents_ = position_ >= reader_size; | 
| + is_cleared_ = position_ > reader_size; | 
| 
 
vmpstr
2017/07/17 21:28:40
nit: This seems like a small difference between ha
 
cblume
2017/07/18 10:23:48
The off-by-one is indeed important.
Having read t
 
vmpstr
2017/07/18 18:02:48
Yeah I meant can you leave a comment somewhere in
 
cblume
2017/07/19 23:27:04
I'll put a comment in the header.
 
 | 
| + } else { | 
| + has_read_all_contents_ = true; | 
| + is_cleared_ = true; | 
| + } | 
| +} | 
| + | 
| +} // namespace blink |