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

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: 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(const skia::RefPtr<SkROBuffer>& buf fer)
12 : m_roBuffer(buffer)
13 , m_positionOfBlock(0)
14 , m_iter(buffer.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.
Peter Kasting 2016/03/23 02:42:59 Nit: Either capitalize Position or wrap it in ||.
scroggo_chromium 2016/03/24 13:59:45 Done.
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 m_positionOfBlock += sizeOfBlock;
45
46 // Move to next block.
47 if (!m_iter.next()) {
Peter Kasting 2016/03/23 02:42:59 Hmm. If this returns false, should we have increm
scroggo_chromium 2016/03/24 13:59:45 Good catch - this is a bug! But the fix is slightl
scroggo_chromium 2016/03/24 22:16:28 I was wrong. This was not a bug. m_roBuffer is con
48 return 0;
49 }
50 }
51 }
52
53 static void unrefRobuffer(const void* ptr, void* context)
54 {
55 auto roBuffer = static_cast<const SkROBuffer*>(context);
Peter Kasting 2016/03/23 02:42:59 Nit: Just inline into next statement
scroggo_chromium 2016/03/24 13:59:45 Done.
56 roBuffer->unref();
57 }
58
59 PassRefPtr<SkData> ROBufferSegmentReader::getAsSkData() const
60 {
61 if (!m_roBuffer)
62 return nullptr;
63
64 // Check to see if the data is already contiguous.
65 SkROBuffer::Iter iter(m_roBuffer.get());
66 const bool multipleBlocks = iter.next();
Peter Kasting 2016/03/23 02:42:59 Nit: Maybe there should be a hasNext() method (or
scroggo_chromium 2016/03/24 13:59:45 That seems reasonable. It would require landing so
67 iter.reset(m_roBuffer.get());
68
69 if (multipleBlocks) {
70 SkData* data = SkData::NewUninitialized(m_roBuffer->size());
71 void* dst = data->writable_data();
Peter Kasting 2016/03/23 02:42:59 Nit: I think there would be less casting if this w
scroggo_chromium 2016/03/24 13:59:45 Done.
72 do {
73 size_t size = iter.size();
74 memcpy(dst, iter.data(), size);
75 dst = static_cast<void*>(static_cast<char*>(dst) + size);
76 } while (iter.next());
77 return adoptRef(data);
78 }
79
80 // Contiguous data. No need to copy.
81 m_roBuffer->ref();
82 return adoptRef(SkData::NewWithProc(iter.data(), iter.size(), &unrefRobuffer ,
Peter Kasting 2016/03/23 02:42:59 Nit: Begin all lines of args at the same position
scroggo_chromium 2016/03/24 13:59:45 This is where the presubmit hooks complained (I tr
83 // uref_robuffer will not modify this, besides the mutable field
84 // fRefCnt, but NewWithProc expects a void* (not const).
85 const_cast<SkROBuffer*>(m_roBuffer.get())));
f(malita) 2016/03/23 16:41:58 SkROBuffer is intrinsically const (I think :), so
scroggo_chromium 2016/03/24 13:59:45 I missed your comment above, but you're right! I'v
86 }
87
88 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698