1/* 2 * Copyright (c) 2013, Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31#include "config.h" 32#include "core/platform/graphics/skia/SkiaSharedBufferStream.h" 33 34#include "third_party/skia/include/core/SkStream.h" 35#include "core/platform/SharedBuffer.h" 36#include <algorithm> 37#include <cstring> 38 39namespace WebCore { 40 41size_t SkiaSharedBufferStream::read(void* buffer, size_t bytesRequested) 42{ 43 const size_t bytesLeft = m_buffer->size() - m_offset; 44 const size_t bytesToConsume = std::min(bytesLeft, bytesRequested); 45 46 if (buffer) { 47 char* byteBuffer = reinterpret_cast<char*>(buffer); 48 unsigned byteOffset = m_offset; 49 unsigned bytesLeftToConsume = bytesToConsume; 50 while (bytesLeftToConsume > 0) { 51 const char* segment; 52 unsigned bytesInSegment = m_buffer->getSomeData(segment, byteOffset); 53 if (!bytesInSegment) { 54 unsigned bytesRead = bytesToConsume - bytesLeftToConsume; 55 m_offset += bytesRead; 56 return bytesRead; 57 } 58 unsigned bytesToCopy = std::min(bytesInSegment, bytesLeftToConsume); 59 std::memcpy(byteBuffer, segment, bytesToCopy); 60 bytesLeftToConsume -= bytesToCopy; 61 byteBuffer += bytesToCopy; 62 byteOffset += bytesToCopy; 63 } 64 } 65 m_offset += bytesToConsume; 66 return bytesToConsume; 67} 68 69bool SkiaSharedBufferStream::isAtEnd() const 70{ 71 return this->getLength() == m_offset; 72} 73 74bool SkiaSharedBufferStream::rewind() 75{ 76 m_offset = 0; 77 return true; 78} 79 80SkiaSharedBufferStream* SkiaSharedBufferStream::duplicate() const 81{ 82 return new SkiaSharedBufferStream(m_buffer); 83} 84 85size_t SkiaSharedBufferStream::getPosition() const 86{ 87 return m_offset; 88} 89 90bool SkiaSharedBufferStream::seek(size_t position) 91{ 92 m_offset = std::min(position, static_cast<size_t>(m_buffer->size())); 93 return true; 94} 95 96bool SkiaSharedBufferStream::move(long offset) 97{ 98 return this->seek(m_offset + offset); 99} 100 101SkiaSharedBufferStream* SkiaSharedBufferStream::fork() const 102{ 103 SkiaSharedBufferStream* that = this->duplicate(); 104 that->m_offset = this->m_offset; 105 return that; 106} 107 108size_t SkiaSharedBufferStream::getLength() const 109{ 110 return m_buffer->size(); 111} 112 113const void* SkiaSharedBufferStream::getMemoryBase() 114{ 115 const char* segment; 116 unsigned bytesInSegment = m_buffer->getSomeData(segment); 117 return (bytesInSegment != m_buffer->size()) ? 0 : segment; 118} 119 120} 121