initial import
[vuplus_webkit] / Source / WebKit2 / Shared / ShareableBitmap.h
1 /*
2  * Copyright (C) 2010, 2011 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 #ifndef ShareableBitmap_h
27 #define ShareableBitmap_h
28
29 #include "SharedMemory.h"
30 #include <WebCore/IntRect.h>
31 #include <wtf/PassOwnPtr.h>
32 #include <wtf/PassRefPtr.h>
33 #include <wtf/RefCounted.h>
34 #include <wtf/RefPtr.h>
35
36 #if USE(CG)
37 #include <wtf/RetainPtr.h>
38 #endif
39
40 #if USE(CAIRO)
41 #include <WebCore/RefPtrCairo.h>
42 #endif
43
44 #if PLATFORM(QT)
45 #include <QImage>
46 #ifdef Q_WS_X11
47 // Avoid ambiguity caused by the Region typedef from qwindowdefs.h.
48 namespace WebCore { class Region; }
49 namespace WebKit { using WebCore::Region; }
50 #endif
51 #endif
52
53 namespace WebCore {
54     class Image;
55     class GraphicsContext;
56 }
57
58 namespace WebKit {
59     
60 class ShareableBitmap : public RefCounted<ShareableBitmap> {
61 public:
62     enum Flag {
63         SupportsAlpha = 1 << 0,
64     };
65     typedef unsigned Flags;
66
67     class Handle {
68         WTF_MAKE_NONCOPYABLE(Handle);
69     public:
70         Handle();
71
72         bool isNull() const { return m_handle.isNull(); }
73
74         void encode(CoreIPC::ArgumentEncoder*) const;
75         static bool decode(CoreIPC::ArgumentDecoder*, Handle&);
76
77     private:
78         friend class ShareableBitmap;
79
80         mutable SharedMemory::Handle m_handle;
81         WebCore::IntSize m_size;
82         Flags m_flags;
83     };
84
85     // Create a shareable bitmap that uses malloced memory.
86     static PassRefPtr<ShareableBitmap> create(const WebCore::IntSize&, Flags);
87
88     // Create a shareable bitmap whose backing memory can be shared with another process.
89     static PassRefPtr<ShareableBitmap> createShareable(const WebCore::IntSize&, Flags);
90
91     // Create a shareable bitmap from an already existing shared memory block.
92     static PassRefPtr<ShareableBitmap> create(const WebCore::IntSize&, Flags, PassRefPtr<SharedMemory>);
93
94     // Create a shareable bitmap from a handle.
95     static PassRefPtr<ShareableBitmap> create(const Handle&);
96
97     // Create a handle.
98     bool createHandle(Handle&);
99
100     ~ShareableBitmap();
101
102     const WebCore::IntSize& size() const { return m_size; }
103     WebCore::IntRect bounds() const { return WebCore::IntRect(WebCore::IntPoint(), size()); }
104
105     bool resize(const WebCore::IntSize& size);
106
107     // Create a graphics context that can be used to paint into the backing store.
108     PassOwnPtr<WebCore::GraphicsContext> createGraphicsContext();
109
110     // Paint the backing store into the given context.
111     void paint(WebCore::GraphicsContext&, const WebCore::IntPoint& destination, const WebCore::IntRect& source);
112     void paint(WebCore::GraphicsContext&, float scaleFactor, const WebCore::IntPoint& destination, const WebCore::IntRect& source);
113
114     bool isBackedBySharedMemory() const { return m_sharedMemory; }
115
116     // This creates a bitmap image that directly references the shared bitmap data.
117     // This is only safe to use when we know that the contents of the shareable bitmap won't change.
118     PassRefPtr<WebCore::Image> createImage();
119
120 #if PLATFORM(WIN)
121     HDC windowsContext() const;
122 #endif
123 #if USE(CG)
124     // This creates a copied CGImageRef (most likely a copy-on-write) of the shareable bitmap.
125     RetainPtr<CGImageRef> makeCGImageCopy();
126
127     // This creates a CGImageRef that directly references the shared bitmap data.
128     // This is only safe to use when we know that the contents of the shareable bitmap won't change.
129     RetainPtr<CGImageRef> makeCGImage();
130 #elif USE(CAIRO)
131     // This creates a BitmapImage that directly references the shared bitmap data.
132     // This is only safe to use when we know that the contents of the shareable bitmap won't change.
133     PassRefPtr<cairo_surface_t> createCairoSurface();
134 #elif PLATFORM(QT)
135     // This creates a QImage that directly references the shared bitmap data.
136     // This is only safe to use when we know that the contents of the shareable bitmap won't change.
137     QImage createQImage();
138 #endif
139
140 private:
141     ShareableBitmap(const WebCore::IntSize&, Flags, void*);
142     ShareableBitmap(const WebCore::IntSize&, Flags, PassRefPtr<SharedMemory>);
143
144     static size_t numBytesForSize(const WebCore::IntSize& size) { return size.width() * size.height() * 4; }
145
146 #if USE(CG)
147     RetainPtr<CGImageRef> createCGImage(CGDataProviderRef) const;
148     static void releaseBitmapContextData(void* typelessBitmap, void* typelessData);
149     static void releaseDataProviderData(void* typelessBitmap, const void* typelessData, size_t);
150 #endif
151
152 #if USE(CAIRO)
153     static void releaseSurfaceData(void* typelessBitmap);
154 #endif
155
156     void* data() const;
157     size_t sizeInBytes() const { return numBytesForSize(m_size); }
158
159     WebCore::IntSize m_size;
160     Flags m_flags;
161
162     // If the shareable bitmap is backed by shared memory, this points to the shared memory object.
163     RefPtr<SharedMemory> m_sharedMemory;
164
165     // If the shareable bitmap is backed by fastMalloced memory, this points to the data.
166     void* m_data;
167
168 #if PLATFORM(WIN)
169     mutable OwnPtr<HDC> m_windowsContext;
170     mutable OwnPtr<HBITMAP> m_windowsBitmap;
171 #endif
172 };
173
174 } // namespace WebKit
175
176 #endif // ShareableBitmap_h