| OLD | NEW |
| (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 |
| OLD | NEW |