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

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

Issue 2565323003: Move gif image decoder to SkCodec (Closed)
Patch Set: Make SegmentStream movable, clean naming Created 3 years, 7 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
(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),
12 has_read_all_contents_(true),
13 is_cleared_(true) {}
14
15 SegmentStream::SegmentStream(SegmentStream&& rhs)
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_;
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_;
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);
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;
104 } else {
105 has_read_all_contents_ = true;
106 is_cleared_ = true;
107 }
108 }
109
110 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698