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

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

Issue 1812273003: Eliminate copies of encoded image data (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Respond to (most of) pkasting's comments in patch set 12 Created 4 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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/SegmentReader.h"
6
7 #include "third_party/skia/src/core/SkRWBuffer.h"
8 #include "wtf/Assertions.h"
9 #include "wtf/Noncopyable.h"
10 #include "wtf/PassRefPtr.h"
11 #include "wtf/RefPtr.h"
12
13 namespace blink {
14
15 class ROBufferSegmentReader final : public SegmentReader {
16 WTF_MAKE_NONCOPYABLE(ROBufferSegmentReader);
17 public:
18 ROBufferSegmentReader(PassRefPtr<SkROBuffer>);
19
20 size_t getSomeData(const char*& data, size_t position) const override;
21 size_t size() const override { return m_roBuffer ? m_roBuffer->size() : 0; }
22 PassRefPtr<SkData> getAsSkData() const override;
23
24 private:
25 RefPtr<SkROBuffer> m_roBuffer;
26 // Position of the first char in the current block of m_iter.
27 mutable size_t m_positionOfBlock;
28 mutable SkROBuffer::Iter m_iter;
29 };
30
31 PassRefPtr<SegmentReader> SegmentReader::createFromSkROBuffer(PassRefPtr<SkROBuf fer> buffer)
32 {
33 return adoptRef(new ROBufferSegmentReader(buffer));
34 }
35
36 ROBufferSegmentReader::ROBufferSegmentReader(PassRefPtr<SkROBuffer> buffer)
37 : m_roBuffer(buffer)
38 , m_positionOfBlock(0)
39 , m_iter(m_roBuffer.get())
40 {}
41
42 size_t ROBufferSegmentReader::getSomeData(const char*& data, size_t position) co nst
43 {
44 if (!m_roBuffer)
45 return 0;
46
47 if (position < m_positionOfBlock) {
48 // SkROBuffer::Iter only iterates forwards. Start from the
49 // beginning.
50 m_iter.reset(m_roBuffer.get());
51 m_positionOfBlock = 0;
52 }
53
54 for (size_t sizeOfBlock = m_iter.size(); sizeOfBlock != 0; m_positionOfBlock += sizeOfBlock) {
55 ASSERT(m_positionOfBlock <= position);
56
57 if (m_positionOfBlock + sizeOfBlock > position) {
58 // |position| is in this block.
59 const size_t positionInBlock = position - m_positionOfBlock;
60 data = static_cast<const char*>(m_iter.data()) + positionInBlock;
61 return sizeOfBlock - positionInBlock;
62 }
63
64 // Move to next block.
65 if (!m_iter.next()) {
66 // Reset to the beginning, so future calls can succeed.
67 m_iter.reset(m_roBuffer.get());
68 m_positionOfBlock = 0;
69 return 0;
70 }
71
72 }
73
74 return 0;
75 }
76
77 static void unrefROBuffer(const void* ptr, void* context)
78 {
79 static_cast<SkROBuffer*>(context)->unref();
80 }
81
82 PassRefPtr<SkData> ROBufferSegmentReader::getAsSkData() const
83 {
84 if (!m_roBuffer)
85 return nullptr;
86
87 // Check to see if the data is already contiguous.
88 SkROBuffer::Iter iter(m_roBuffer.get());
89 const bool multipleBlocks = iter.next();
90 iter.reset(m_roBuffer.get());
91
92 if (!multipleBlocks) {
93 // Contiguous data. No need to copy.
94 m_roBuffer->ref();
95 return adoptRef(SkData::NewWithProc(iter.data(), iter.size(), &unrefROBu ffer, m_roBuffer.get()));
96 }
97
98 SkData* data = SkData::NewUninitialized(m_roBuffer->size());
scroggo 2016/03/25 15:49:53 Updating this to a RefPtr...
99 char* dst = static_cast<char*>(data->writable_data());
100 do {
101 size_t size = iter.size();
102 memcpy(dst, iter.data(), size);
103 dst += size;
104 } while (iter.next());
105 return adoptRef(data);
106 }
107
108 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698