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 "ShareableBitmap.h"
28
29 #include "SharedMemory.h"
30 #include "WebCoreArgumentCoders.h"
31 #include <WebCore/GraphicsContext.h>
32
33 using namespace WebCore;
34
35 namespace WebKit {
36
Handle()37 ShareableBitmap::Handle::Handle()
38 : m_flags(0)
39 {
40 }
41
encode(CoreIPC::ArgumentEncoder * encoder) const42 void ShareableBitmap::Handle::encode(CoreIPC::ArgumentEncoder* encoder) const
43 {
44 encoder->encode(m_handle);
45 encoder->encode(m_size);
46 encoder->encode(m_flags);
47 }
48
decode(CoreIPC::ArgumentDecoder * decoder,Handle & handle)49 bool ShareableBitmap::Handle::decode(CoreIPC::ArgumentDecoder* decoder, Handle& handle)
50 {
51 if (!decoder->decode(handle.m_handle))
52 return false;
53 if (!decoder->decode(handle.m_size))
54 return false;
55 if (!decoder->decode(handle.m_flags))
56 return false;
57 return true;
58 }
59
create(const IntSize & size,Flags flags)60 PassRefPtr<ShareableBitmap> ShareableBitmap::create(const IntSize& size, Flags flags)
61 {
62 size_t numBytes = numBytesForSize(size);
63
64 void* data = 0;
65 if (!tryFastMalloc(numBytes).getValue(data))
66 return 0;
67
68 return adoptRef(new ShareableBitmap(size, flags, data));
69 }
70
createShareable(const IntSize & size,Flags flags)71 PassRefPtr<ShareableBitmap> ShareableBitmap::createShareable(const IntSize& size, Flags flags)
72 {
73 size_t numBytes = numBytesForSize(size);
74
75 RefPtr<SharedMemory> sharedMemory = SharedMemory::create(numBytes);
76 if (!sharedMemory)
77 return 0;
78
79 return adoptRef(new ShareableBitmap(size, flags, sharedMemory));
80 }
81
create(const IntSize & size,Flags flags,PassRefPtr<SharedMemory> sharedMemory)82 PassRefPtr<ShareableBitmap> ShareableBitmap::create(const IntSize& size, Flags flags, PassRefPtr<SharedMemory> sharedMemory)
83 {
84 ASSERT(sharedMemory);
85
86 size_t numBytes = numBytesForSize(size);
87 ASSERT_UNUSED(numBytes, sharedMemory->size() >= numBytes);
88
89 return adoptRef(new ShareableBitmap(size, flags, sharedMemory));
90 }
91
create(const Handle & handle)92 PassRefPtr<ShareableBitmap> ShareableBitmap::create(const Handle& handle)
93 {
94 // Create the shared memory.
95 RefPtr<SharedMemory> sharedMemory = SharedMemory::create(handle.m_handle, SharedMemory::ReadWrite);
96 if (!sharedMemory)
97 return 0;
98
99 return create(handle.m_size, handle.m_flags, sharedMemory.release());
100 }
101
createHandle(Handle & handle)102 bool ShareableBitmap::createHandle(Handle& handle)
103 {
104 ASSERT(isBackedBySharedMemory());
105
106 if (!m_sharedMemory->createHandle(handle.m_handle, SharedMemory::ReadWrite))
107 return false;
108 handle.m_size = m_size;
109 handle.m_flags = m_flags;
110 return true;
111 }
112
ShareableBitmap(const IntSize & size,Flags flags,void * data)113 ShareableBitmap::ShareableBitmap(const IntSize& size, Flags flags, void* data)
114 : m_size(size)
115 , m_flags(flags)
116 , m_data(data)
117 {
118 }
119
ShareableBitmap(const IntSize & size,Flags flags,PassRefPtr<SharedMemory> sharedMemory)120 ShareableBitmap::ShareableBitmap(const IntSize& size, Flags flags, PassRefPtr<SharedMemory> sharedMemory)
121 : m_size(size)
122 , m_flags(flags)
123 , m_sharedMemory(sharedMemory)
124 , m_data(0)
125 {
126 }
127
~ShareableBitmap()128 ShareableBitmap::~ShareableBitmap()
129 {
130 if (!isBackedBySharedMemory())
131 fastFree(m_data);
132 }
133
resize(const IntSize & size)134 bool ShareableBitmap::resize(const IntSize& size)
135 {
136 // We can't resize backing stores that are backed by shared memory.
137 ASSERT(!isBackedBySharedMemory());
138
139 if (size == m_size)
140 return true;
141
142 size_t newNumBytes = numBytesForSize(size);
143
144 // Try to resize.
145 char* newData = 0;
146 if (!tryFastRealloc(m_data, newNumBytes).getValue(newData)) {
147 // We failed, but the backing store is still kept in a consistent state.
148 return false;
149 }
150
151 m_size = size;
152 m_data = newData;
153
154 return true;
155 }
156
data() const157 void* ShareableBitmap::data() const
158 {
159 if (isBackedBySharedMemory())
160 return m_sharedMemory->data();
161
162 ASSERT(m_data);
163 return m_data;
164 }
165
166 } // namespace WebKit
167