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

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: Convert to PassRefPtr in ImageDecoder, helper for setData Created 4 years, 9 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/ROBufferSegmentReader.h"
6
7 #include "wtf/Assertions.h"
8
9 namespace blink {
10
11 ROBufferSegmentReader::ROBufferSegmentReader(PassRefPtr<SkROBuffer> buffer)
12 : m_roBuffer(buffer)
13 , m_positionOfBlock(0)
14 , m_iter(m_roBuffer.get())
15 {}
16
17 size_t ROBufferSegmentReader::getSomeData(const char*& data, size_t position) co nst
18 {
19 if (!m_roBuffer)
20 return 0;
21
22 if (position < m_positionOfBlock) {
23 // SkROBuffer::Iter only iterates forwards. Start from the
24 // beginning.
25 m_iter.reset(m_roBuffer.get());
26 m_positionOfBlock = 0;
27 }
28
29 while (true) {
30 ASSERT(m_positionOfBlock <= position);
31
32 const size_t sizeOfBlock = m_iter.size();
33 if (sizeOfBlock == 0) {
34 return 0;
35 }
36
37 if (m_positionOfBlock + sizeOfBlock > position) {
38 // |position| is in this block.
39 const size_t positionInBlock = position - m_positionOfBlock;
40 data = static_cast<const char*>(m_iter.data()) + positionInBlock;
41 return sizeOfBlock - positionInBlock;
42 }
43
44 // Move to next block.
45 if (!m_iter.next()) {
46 // Reset to the beginning, so future calls can succeed.
47 m_iter.reset(m_roBuffer.get());
48 m_positionOfBlock = 0;
49 return 0;
50 }
51
52 m_positionOfBlock += sizeOfBlock;
53 }
54 }
55
56 static void unrefRobuffer(const void* ptr, void* context)
57 {
58 static_cast<SkROBuffer*>(context)->unref();
59 }
60
61 PassRefPtr<SkData> ROBufferSegmentReader::getAsSkData() const
62 {
63 if (!m_roBuffer)
64 return nullptr;
65
66 // Check to see if the data is already contiguous.
67 SkROBuffer::Iter iter(m_roBuffer.get());
68 const bool multipleBlocks = iter.next();
69 iter.reset(m_roBuffer.get());
70
71 if (multipleBlocks) {
72 SkData* data = SkData::NewUninitialized(m_roBuffer->size());
73 char* dst = static_cast<char*>(data->writable_data());
74 do {
75 size_t size = iter.size();
76 memcpy(dst, iter.data(), size);
77 dst += size;
78 } while (iter.next());
79 return adoptRef(data);
80 }
81
82 // Contiguous data. No need to copy.
83 m_roBuffer->ref();
84 return adoptRef(SkData::NewWithProc(iter.data(), iter.size(), &unrefRobuffer , m_roBuffer.get()));
85 }
86
87 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698