2 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "ShareableBitmap.h"
29 #include "SharedMemory.h"
30 #include "WebCoreArgumentCoders.h"
31 #include <WebCore/GraphicsContext.h>
33 using namespace WebCore;
37 ShareableBitmap::Handle::Handle()
42 void ShareableBitmap::Handle::encode(CoreIPC::ArgumentEncoder* encoder) const
44 encoder->encode(m_handle);
45 encoder->encode(m_size);
46 encoder->encode(m_flags);
49 bool ShareableBitmap::Handle::decode(CoreIPC::ArgumentDecoder* decoder, Handle& handle)
51 if (!decoder->decode(handle.m_handle))
53 if (!decoder->decode(handle.m_size))
55 if (!decoder->decode(handle.m_flags))
60 PassRefPtr<ShareableBitmap> ShareableBitmap::create(const IntSize& size, Flags flags)
62 size_t numBytes = numBytesForSize(size);
65 if (!tryFastMalloc(numBytes).getValue(data))
68 return adoptRef(new ShareableBitmap(size, flags, data));
71 PassRefPtr<ShareableBitmap> ShareableBitmap::createShareable(const IntSize& size, Flags flags)
73 size_t numBytes = numBytesForSize(size);
75 RefPtr<SharedMemory> sharedMemory = SharedMemory::create(numBytes);
79 return adoptRef(new ShareableBitmap(size, flags, sharedMemory));
82 PassRefPtr<ShareableBitmap> ShareableBitmap::create(const IntSize& size, Flags flags, PassRefPtr<SharedMemory> sharedMemory)
86 size_t numBytes = numBytesForSize(size);
87 ASSERT_UNUSED(numBytes, sharedMemory->size() >= numBytes);
89 return adoptRef(new ShareableBitmap(size, flags, sharedMemory));
92 PassRefPtr<ShareableBitmap> ShareableBitmap::create(const Handle& handle)
94 // Create the shared memory.
95 RefPtr<SharedMemory> sharedMemory = SharedMemory::create(handle.m_handle, SharedMemory::ReadWrite);
99 return create(handle.m_size, handle.m_flags, sharedMemory.release());
102 bool ShareableBitmap::createHandle(Handle& handle)
104 ASSERT(isBackedBySharedMemory());
106 if (!m_sharedMemory->createHandle(handle.m_handle, SharedMemory::ReadWrite))
108 handle.m_size = m_size;
109 handle.m_flags = m_flags;
113 ShareableBitmap::ShareableBitmap(const IntSize& size, Flags flags, void* data)
120 ShareableBitmap::ShareableBitmap(const IntSize& size, Flags flags, PassRefPtr<SharedMemory> sharedMemory)
123 , m_sharedMemory(sharedMemory)
128 ShareableBitmap::~ShareableBitmap()
130 if (!isBackedBySharedMemory())
134 bool ShareableBitmap::resize(const IntSize& size)
136 // We can't resize backing stores that are backed by shared memory.
137 ASSERT(!isBackedBySharedMemory());
142 size_t newNumBytes = numBytesForSize(size);
146 if (!tryFastRealloc(m_data, newNumBytes).getValue(newData)) {
147 // We failed, but the backing store is still kept in a consistent state.
157 void* ShareableBitmap::data() const
159 if (isBackedBySharedMemory())
160 return m_sharedMemory->data();
166 } // namespace WebKit