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 "config.h"
27#include "UpdateChunk.h"
28
29#include "ArgumentDecoder.h"
30#include "ArgumentEncoder.h"
31#include "Attachment.h"
32#include "WebCoreArgumentCoders.h"
33#include <WebCore/FloatRect.h>
34#include <mach/vm_map.h>
35#include <wtf/RetainPtr.h>
36
37using namespace WebCore;
38
39namespace WebKit {
40
41UpdateChunk::UpdateChunk()
42    : m_data(0)
43    , m_size(0)
44{
45}
46
47UpdateChunk::UpdateChunk(const IntRect& rect)
48    : m_rect(rect)
49    , m_size(size())
50{
51    vm_allocate(mach_task_self(), reinterpret_cast<vm_address_t*>(&m_data), m_size, VM_FLAGS_ANYWHERE);
52}
53
54UpdateChunk::~UpdateChunk()
55{
56    if (m_data)
57        vm_deallocate(mach_task_self(), reinterpret_cast<vm_address_t>(m_data), m_size);
58}
59
60RetainPtr<CGImageRef> UpdateChunk::createImage()
61{
62    RetainPtr<CGDataProviderRef> provider(AdoptCF, CGDataProviderCreateWithData(0, m_data, size(), 0));
63    RetainPtr<CGColorSpaceRef> colorSpace(AdoptCF, CGColorSpaceCreateDeviceRGB());
64    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));
65
66    return image;
67}
68
69void UpdateChunk::encode(CoreIPC::ArgumentEncoder* encoder) const
70{
71    encoder->encode(m_rect);
72    encoder->encode(CoreIPC::Attachment(m_data, size(), MACH_MSG_VIRTUAL_COPY, true));
73
74    m_data = 0;
75}
76
77bool UpdateChunk::decode(CoreIPC::ArgumentDecoder* decoder, UpdateChunk& chunk)
78{
79    IntRect rect;
80    if (!decoder->decode(rect))
81        return false;
82    chunk.m_rect = rect;
83
84    CoreIPC::Attachment attachment;
85    if (!decoder->decode(attachment))
86        return false;
87
88    chunk.m_size = attachment.size();
89    chunk.m_data = reinterpret_cast<uint8_t*>(attachment.address());
90    return true;
91}
92
93} // namespace WebKit
94