1 /*
2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 * Portions Copyright (c) 2010 Motorola Mobility, Inc. All rights reserved.
4 * Copyright (C) 2011 Igalia S.L
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 "WebCoreArgumentCoders.h"
32 #include <gdk/gdkx.h>
33
34 using namespace WebCore;
35
36 static cairo_format_t imageFormat = CAIRO_FORMAT_ARGB32;
37
38 namespace WebKit {
39
UpdateChunk()40 UpdateChunk::UpdateChunk()
41 : m_sharedMemory(0)
42 {
43 }
44
UpdateChunk(const IntRect & rect)45 UpdateChunk::UpdateChunk(const IntRect& rect)
46 : m_rect(rect)
47 , m_sharedMemory(SharedMemory::create(size()))
48 {
49 }
50
~UpdateChunk()51 UpdateChunk::~UpdateChunk()
52 {
53 }
54
size() const55 size_t UpdateChunk::size() const
56 {
57 return cairo_format_stride_for_width(imageFormat, m_rect.width()) * m_rect.height();
58 }
59
encode(CoreIPC::ArgumentEncoder * encoder) const60 void UpdateChunk::encode(CoreIPC::ArgumentEncoder* encoder) const
61 {
62 encoder->encode(m_rect);
63 if (!m_sharedMemory) {
64 encoder->encode(false);
65 return;
66 }
67
68 SharedMemory::Handle handle;
69 if (m_sharedMemory->createHandle(handle, SharedMemory::ReadOnly)) {
70 encoder->encode(true);
71 encoder->encode(handle);
72 } else
73 encoder->encode(false);
74
75 m_sharedMemory = 0;
76 }
77
decode(CoreIPC::ArgumentDecoder * decoder,UpdateChunk & chunk)78 bool UpdateChunk::decode(CoreIPC::ArgumentDecoder* decoder, UpdateChunk& chunk)
79 {
80 ASSERT_ARG(chunk, chunk.isEmpty());
81
82 IntRect rect;
83 if (!decoder->decode(rect))
84 return false;
85
86 chunk.m_rect = rect;
87
88 bool hasSharedMemory;
89 if (!decoder->decode(hasSharedMemory))
90 return false;
91
92 if (!hasSharedMemory) {
93 chunk.m_sharedMemory = 0;
94 return true;
95 }
96
97 SharedMemory::Handle handle;
98 if (!decoder->decode(handle))
99 return false;
100
101 chunk.m_sharedMemory = SharedMemory::create(handle, SharedMemory::ReadOnly);
102 return true;
103 }
104
createImage() const105 cairo_surface_t* UpdateChunk::createImage() const
106 {
107 ASSERT(m_sharedMemory);
108 if (!m_sharedMemory)
109 return 0;
110
111 int stride = cairo_format_stride_for_width(imageFormat, m_rect.width());
112 return cairo_image_surface_create_for_data(static_cast<unsigned char*>(m_sharedMemory->data()),
113 imageFormat, m_rect.width(), m_rect.height(), stride);
114 }
115
116 } // namespace WebKit
117