1/* 2 * Copyright (C) 2010 Apple Inc. All rights reserved. 3 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) 4 * Copyright (C) 2010 University of Szeged 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 17 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS 19 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 25 * THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28#include "config.h" 29#include "UpdateChunk.h" 30 31#include "ArgumentDecoder.h" 32#include "ArgumentEncoder.h" 33#include "WebCoreArgumentCoders.h" 34#include <QIODevice> 35#include <QImage> 36#include <QPixmap> 37#include <WebCore/FloatRect.h> 38#include <wtf/text/WTFString.h> 39 40using namespace WebCore; 41using namespace std; 42 43namespace WebKit { 44 45UpdateChunk::UpdateChunk() 46{ 47} 48 49UpdateChunk::UpdateChunk(const IntRect& rect) 50 : m_rect(rect) 51 , m_sharedMemory(SharedMemory::create(size())) 52{ 53} 54 55UpdateChunk::~UpdateChunk() 56{ 57} 58 59void UpdateChunk::encode(CoreIPC::ArgumentEncoder* encoder) const 60{ 61 encoder->encode(m_rect); 62 if (!m_sharedMemory) { 63 encoder->encode(false); 64 return; 65 } 66 67 SharedMemory::Handle handle; 68 if (m_sharedMemory->createHandle(handle, SharedMemory::ReadOnly)) { 69 encoder->encode(true); 70 encoder->encode(handle); 71 } else 72 encoder->encode(false); 73 74 m_sharedMemory = 0; 75} 76 77bool UpdateChunk::decode(CoreIPC::ArgumentDecoder* decoder, UpdateChunk& chunk) 78{ 79 ASSERT_ARG(chunk, chunk.isEmpty()); 80 81 IntRect rect; 82 if (!decoder->decode(rect)) 83 return false; 84 85 chunk.m_rect = rect; 86 87 bool hasSharedMemory; 88 if (!decoder->decode(hasSharedMemory)) 89 return false; 90 91 if (!hasSharedMemory) { 92 chunk.m_sharedMemory = 0; 93 return true; 94 } 95 96 SharedMemory::Handle handle; 97 if (!decoder->decode(handle)) 98 return false; 99 100 chunk.m_sharedMemory = SharedMemory::create(handle, SharedMemory::ReadOnly); 101 return true; 102} 103 104size_t UpdateChunk::size() const 105{ 106 int bpp; 107 if (QPixmap::defaultDepth() == 16) 108 bpp = 2; 109 else 110 bpp = 4; 111 112 return ((m_rect.width() * bpp + 3) & ~0x3) 113 * m_rect.height(); 114} 115 116QImage UpdateChunk::createImage() const 117{ 118 ASSERT(m_sharedMemory); 119 if (!m_sharedMemory) 120 return QImage(); 121 122 QImage::Format format; 123 int bpp; 124 if (QPixmap::defaultDepth() == 16) { 125 format = QImage::Format_RGB16; 126 bpp = 2; 127 } else { 128 format = QImage::Format_RGB32; 129 bpp = 4; 130 } 131 132 return QImage(reinterpret_cast<unsigned char*>(m_sharedMemory->data()), m_rect.width(), m_rect.height(), (m_rect.width() * bpp + 3) & ~0x3, format); 133} 134 135} // namespace WebKit 136