Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "platform/image-decoders/SegmentStream.h" | |
| 6 | |
| 7 namespace blink { | |
| 8 | |
| 9 SegmentStream::SegmentStream() | |
| 10 : reader_(), | |
| 11 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.
| |
| 12 has_read_all_contents_(true), | |
| 13 is_cleared_(true) {} | |
| 14 | |
| 15 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
| |
| 16 : reader_(rhs.reader_), | |
| 17 position_(rhs.position_), | |
| 18 has_read_all_contents_(rhs.has_read_all_contents_), | |
| 19 is_cleared_(rhs.is_cleared_) {} | |
| 20 | |
| 21 SegmentStream& SegmentStream::operator=(SegmentStream&& rhs) { | |
| 22 reader_ = rhs.reader_; | |
|
vmpstr
2017/07/17 21:28:40
nit: reader_ = std::move(rhs.reader_);
cblume
2017/07/18 10:23:48
Done.
| |
| 23 position_ = rhs.position_; | |
| 24 has_read_all_contents_ = rhs.has_read_all_contents_; | |
| 25 is_cleared_ = rhs.is_cleared_; | |
| 26 | |
| 27 return *this; | |
| 28 } | |
| 29 | |
| 30 void SegmentStream::SetReader(WTF::PassRefPtr<SegmentReader> reader) { | |
| 31 reader_ = std::move(reader); | |
| 32 SetPositionState(position_); | |
| 33 } | |
| 34 | |
| 35 size_t SegmentStream::read(void* buffer, size_t size) { | |
| 36 if (is_cleared_) | |
| 37 return 0; | |
| 38 | |
| 39 size = std::min(size, reader_->size() - position_); | |
| 40 | |
| 41 size_t bytes_advanced = 0; | |
| 42 if (!buffer) { // skipping, not reading | |
| 43 bytes_advanced = size; | |
| 44 } else { | |
| 45 bytes_advanced = peek(buffer, size); | |
| 46 } | |
| 47 | |
| 48 SetPositionState(position_ + bytes_advanced); | |
| 49 | |
| 50 return bytes_advanced; | |
| 51 } | |
| 52 | |
| 53 size_t SegmentStream::peek(void* buffer, size_t size) const { | |
| 54 if (is_cleared_) | |
| 55 return 0; | |
| 56 | |
| 57 size = std::min(size, reader_->size() - position_); | |
| 58 | |
| 59 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.
| |
| 60 size_t total_bytes_peeked = 0; | |
| 61 char* buffer_as_char_ptr = reinterpret_cast<char*>(buffer); | |
| 62 while (size) { | |
| 63 const char* segment = nullptr; | |
| 64 size_t bytes_peeked = reader_->GetSomeData(segment, peek_position); | |
| 65 if (!bytes_peeked) | |
| 66 break; | |
| 67 if (bytes_peeked > size) | |
| 68 bytes_peeked = size; | |
| 69 | |
| 70 memcpy(buffer_as_char_ptr, segment, bytes_peeked); | |
| 71 buffer_as_char_ptr += bytes_peeked; | |
| 72 size -= bytes_peeked; | |
| 73 total_bytes_peeked += bytes_peeked; | |
| 74 peek_position += bytes_peeked; | |
| 75 } | |
| 76 | |
| 77 return total_bytes_peeked; | |
| 78 } | |
| 79 | |
| 80 bool SegmentStream::rewind() { | |
| 81 SetPositionState(0); | |
| 82 return true; | |
| 83 } | |
| 84 | |
| 85 bool SegmentStream::seek(size_t position) { | |
| 86 SetPositionState(position); | |
| 87 return true; | |
| 88 } | |
| 89 | |
| 90 bool SegmentStream::move(long offset) { | |
| 91 long absolute_position = position_ + offset; | |
| 92 | |
| 93 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.
| |
| 94 SetPositionState(absolute_position); | |
| 95 return true; | |
| 96 } | |
| 97 | |
| 98 void SegmentStream::SetPositionState(size_t new_position) { | |
| 99 position_ = new_position; | |
| 100 if (reader_) { | |
| 101 const size_t reader_size = reader_->size(); | |
| 102 has_read_all_contents_ = position_ >= reader_size; | |
| 103 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.
| |
| 104 } else { | |
| 105 has_read_all_contents_ = true; | |
| 106 is_cleared_ = true; | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 } // namespace blink | |
| OLD | NEW |