UpdateChunk.cpp revision 65f03d4f644ce73618e5f4f50dd694b26f55ae12
1/*
2 * Copyright (C) 2010 Apple 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
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "UpdateChunk.h"
27
28#include "ArgumentDecoder.h"
29#include "ArgumentEncoder.h"
30#include "Attachment.h"
31#include "WebCoreArgumentCoders.h"
32#include <WebCore/FloatRect.h>
33#include <mach/vm_map.h>
34#include <wtf/RetainPtr.h>
35
36using namespace WebCore;
37
38namespace WebKit {
39
40UpdateChunk::UpdateChunk()
41    : m_data(0)
42    , m_size(0)
43{
44}
45
46UpdateChunk::UpdateChunk(const IntRect& rect)
47    : m_rect(rect)
48    , m_size(size())
49{
50    vm_allocate(mach_task_self(), reinterpret_cast<vm_address_t*>(&m_data), m_size, VM_FLAGS_ANYWHERE);
51}
52
53UpdateChunk::~UpdateChunk()
54{
55    if (m_data)
56        vm_deallocate(mach_task_self(), reinterpret_cast<vm_address_t>(m_data), m_size);
57}
58
59RetainPtr<CGImageRef> UpdateChunk::createImage()
60{
61    RetainPtr<CGDataProviderRef> provider(AdoptCF, CGDataProviderCreateWithData(0, m_data, size(), 0));
62    RetainPtr<CGColorSpaceRef> colorSpace(AdoptCF, CGColorSpaceCreateDeviceRGB());
63    RetainPtr<CGImageRef> image(AdoptCF, CGImageCreate(m_rect.width(), m_rect.height(), 8, 32, m_rect.width() * 4, colorSpace.get(), kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host, provider.get(), 0, false, kCGRenderingIntentDefault));
64
65    return image;
66}
67
68void UpdateChunk::encode(CoreIPC::ArgumentEncoder* encoder) const
69{
70    encoder->encode(m_rect);
71    encoder->encode(CoreIPC::Attachment(m_data, size(), MACH_MSG_VIRTUAL_COPY, true));
72
73    m_data = 0;
74}
75
76bool UpdateChunk::decode(CoreIPC::ArgumentDecoder* decoder, UpdateChunk& chunk)
77{
78    IntRect rect;
79    if (!decoder->decode(rect))
80        return false;
81    chunk.m_rect = rect;
82
83    CoreIPC::Attachment attachment;
84    if (!decoder->decode(attachment))
85        return false;
86
87    chunk.m_size = attachment.size();
88    chunk.m_data = reinterpret_cast<uint8_t*>(attachment.address());
89    return true;
90}
91
92} // namespace WebKit
93