Chromium Code Reviews| Index: third_party/WebKit/Source/platform/image-decoders/ROBufferSegmentReader.cpp |
| diff --git a/third_party/WebKit/Source/platform/image-decoders/ROBufferSegmentReader.cpp b/third_party/WebKit/Source/platform/image-decoders/ROBufferSegmentReader.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..77901ab3b69ad114706561933e993adc66c530af |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/platform/image-decoders/ROBufferSegmentReader.cpp |
| @@ -0,0 +1,88 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "platform/image-decoders/ROBufferSegmentReader.h" |
| + |
| +#include "wtf/Assertions.h" |
| + |
| +namespace blink { |
| + |
| +ROBufferSegmentReader::ROBufferSegmentReader(const skia::RefPtr<SkROBuffer>& buffer) |
| + : m_roBuffer(buffer) |
| + , m_positionOfBlock(0) |
| + , m_iter(buffer.get()) |
| + {} |
| + |
| +size_t ROBufferSegmentReader::getSomeData(const char*& data, size_t position) const |
| +{ |
| + if (!m_roBuffer) |
| + return 0; |
| + |
| + if (position < m_positionOfBlock) { |
| + // SkROBuffer::Iter only iterates forwards. Start from the |
| + // beginning. |
| + m_iter.reset(m_roBuffer.get()); |
| + m_positionOfBlock = 0; |
| + } |
| + |
| + while (true) { |
| + ASSERT(m_positionOfBlock <= position); |
| + |
| + const size_t sizeOfBlock = m_iter.size(); |
| + if (sizeOfBlock == 0) { |
| + return 0; |
| + } |
| + |
| + if (m_positionOfBlock + sizeOfBlock > position) { |
| + // 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.
|
| + const size_t positionInBlock = position - m_positionOfBlock; |
| + data = static_cast<const char*>(m_iter.data()) + positionInBlock; |
| + return sizeOfBlock - positionInBlock; |
| + } |
| + |
| + m_positionOfBlock += sizeOfBlock; |
| + |
| + // Move to next block. |
| + 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
|
| + return 0; |
| + } |
| + } |
| +} |
| + |
| +static void unrefRobuffer(const void* ptr, void* context) |
| +{ |
| + 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.
|
| + roBuffer->unref(); |
| +} |
| + |
| +PassRefPtr<SkData> ROBufferSegmentReader::getAsSkData() const |
| +{ |
| + if (!m_roBuffer) |
| + return nullptr; |
| + |
| + // Check to see if the data is already contiguous. |
| + SkROBuffer::Iter iter(m_roBuffer.get()); |
| + 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
|
| + iter.reset(m_roBuffer.get()); |
| + |
| + if (multipleBlocks) { |
| + SkData* data = SkData::NewUninitialized(m_roBuffer->size()); |
| + 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.
|
| + do { |
| + size_t size = iter.size(); |
| + memcpy(dst, iter.data(), size); |
| + dst = static_cast<void*>(static_cast<char*>(dst) + size); |
| + } while (iter.next()); |
| + return adoptRef(data); |
| + } |
| + |
| + // Contiguous data. No need to copy. |
| + m_roBuffer->ref(); |
| + 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
|
| + // uref_robuffer will not modify this, besides the mutable field |
| + // fRefCnt, but NewWithProc expects a void* (not const). |
| + 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
|
| +} |
| + |
| +} // namespace blink |