| 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..c2216fc8cf3a919eb127847edcc19efe75a13e9b
|
| --- /dev/null
|
| +++ b/third_party/WebKit/Source/platform/image-decoders/ROBufferSegmentReader.cpp
|
| @@ -0,0 +1,87 @@
|
| +// 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(PassRefPtr<SkROBuffer> buffer)
|
| + : m_roBuffer(buffer)
|
| + , m_positionOfBlock(0)
|
| + , m_iter(m_roBuffer.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.
|
| + const size_t positionInBlock = position - m_positionOfBlock;
|
| + data = static_cast<const char*>(m_iter.data()) + positionInBlock;
|
| + return sizeOfBlock - positionInBlock;
|
| + }
|
| +
|
| + // Move to next block.
|
| + if (!m_iter.next()) {
|
| + // Reset to the beginning, so future calls can succeed.
|
| + m_iter.reset(m_roBuffer.get());
|
| + m_positionOfBlock = 0;
|
| + return 0;
|
| + }
|
| +
|
| + m_positionOfBlock += sizeOfBlock;
|
| + }
|
| +}
|
| +
|
| +static void unrefRobuffer(const void* ptr, void* context)
|
| +{
|
| + static_cast<SkROBuffer*>(context)->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();
|
| + iter.reset(m_roBuffer.get());
|
| +
|
| + if (multipleBlocks) {
|
| + SkData* data = SkData::NewUninitialized(m_roBuffer->size());
|
| + char* dst = static_cast<char*>(data->writable_data());
|
| + do {
|
| + size_t size = iter.size();
|
| + memcpy(dst, iter.data(), size);
|
| + 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, m_roBuffer.get()));
|
| +}
|
| +
|
| +} // namespace blink
|
|
|