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