OSDN Git Service

Merge WebKit at r78450: Initial merge by git.
[android-x86/external-webkit.git] / Source / WebKit2 / Shared / ShareableBitmap.cpp
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 <WebCore/GraphicsContext.h>
31
32 using namespace WebCore;
33
34 namespace WebKit {
35
36 PassRefPtr<ShareableBitmap> ShareableBitmap::create(const WebCore::IntSize& size)
37 {
38     size_t numBytes = numBytesForSize(size);
39     
40     void* data = 0;
41     if (!tryFastMalloc(numBytes).getValue(data))
42         return 0;
43
44     return adoptRef(new ShareableBitmap(size, data));
45 }
46
47 PassRefPtr<ShareableBitmap> ShareableBitmap::createShareable(const IntSize& size)
48 {
49     size_t numBytes = numBytesForSize(size);
50     
51     RefPtr<SharedMemory> sharedMemory = SharedMemory::create(numBytes);
52     if (!sharedMemory)
53         return 0;
54     
55     return adoptRef(new ShareableBitmap(size, sharedMemory));
56 }
57
58 PassRefPtr<ShareableBitmap> ShareableBitmap::create(const WebCore::IntSize& size, const SharedMemory::Handle& handle)
59 {
60     // Create the shared memory.
61     RefPtr<SharedMemory> sharedMemory = SharedMemory::create(handle, SharedMemory::ReadWrite);
62     if (!sharedMemory)
63         return 0;
64
65     size_t numBytes = numBytesForSize(size);
66     ASSERT_UNUSED(numBytes, sharedMemory->size() >= numBytes);
67
68     return adoptRef(new ShareableBitmap(size, sharedMemory));
69 }
70
71 bool ShareableBitmap::createHandle(SharedMemory::Handle& handle)
72 {
73     ASSERT(isBackedBySharedMemory());
74
75     return m_sharedMemory->createHandle(handle, SharedMemory::ReadWrite);
76 }
77
78 ShareableBitmap::ShareableBitmap(const IntSize& size, void* data)
79     : m_size(size)
80     , m_data(data)
81 {
82 }
83
84 ShareableBitmap::ShareableBitmap(const IntSize& size, PassRefPtr<SharedMemory> sharedMemory)
85     : m_size(size)
86     , m_sharedMemory(sharedMemory)
87     , m_data(0)
88 {
89 }
90
91 ShareableBitmap::~ShareableBitmap()
92 {
93     if (!isBackedBySharedMemory())
94         fastFree(m_data);
95 }
96
97 bool ShareableBitmap::resize(const IntSize& size)
98 {
99     // We can't resize backing stores that are backed by shared memory.
100     ASSERT(!isBackedBySharedMemory());
101
102     if (size == m_size)
103         return true;
104
105     size_t newNumBytes = numBytesForSize(size);
106     
107     // Try to resize.
108     char* newData = 0;
109     if (!tryFastRealloc(m_data, newNumBytes).getValue(newData)) {
110         // We failed, but the backing store is still kept in a consistent state.
111         return false;
112     }
113
114     m_size = size;
115     m_data = newData;
116
117     return true;
118 }
119
120 void* ShareableBitmap::data() const
121 {
122     if (isBackedBySharedMemory())
123         return m_sharedMemory->data();
124
125     ASSERT(m_data);
126     return m_data;
127 }
128
129 } // namespace WebKit