2 * Copyright (C) 2010, 2011 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.
26 #ifndef ShareableBitmap_h
27 #define ShareableBitmap_h
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>
37 #include <wtf/RetainPtr.h>
41 #include <WebCore/RefPtrCairo.h>
47 // Avoid ambiguity caused by the Region typedef from qwindowdefs.h.
48 namespace WebCore { class Region; }
49 namespace WebKit { using WebCore::Region; }
55 class GraphicsContext;
60 class ShareableBitmap : public RefCounted<ShareableBitmap> {
63 SupportsAlpha = 1 << 0,
65 typedef unsigned Flags;
68 WTF_MAKE_NONCOPYABLE(Handle);
72 bool isNull() const { return m_handle.isNull(); }
74 void encode(CoreIPC::ArgumentEncoder*) const;
75 static bool decode(CoreIPC::ArgumentDecoder*, Handle&);
78 friend class ShareableBitmap;
80 mutable SharedMemory::Handle m_handle;
81 WebCore::IntSize m_size;
85 // Create a shareable bitmap that uses malloced memory.
86 static PassRefPtr<ShareableBitmap> create(const WebCore::IntSize&, Flags);
88 // Create a shareable bitmap whose backing memory can be shared with another process.
89 static PassRefPtr<ShareableBitmap> createShareable(const WebCore::IntSize&, Flags);
91 // Create a shareable bitmap from an already existing shared memory block.
92 static PassRefPtr<ShareableBitmap> create(const WebCore::IntSize&, Flags, PassRefPtr<SharedMemory>);
94 // Create a shareable bitmap from a handle.
95 static PassRefPtr<ShareableBitmap> create(const Handle&);
98 bool createHandle(Handle&);
102 const WebCore::IntSize& size() const { return m_size; }
103 WebCore::IntRect bounds() const { return WebCore::IntRect(WebCore::IntPoint(), size()); }
105 bool resize(const WebCore::IntSize& size);
107 // Create a graphics context that can be used to paint into the backing store.
108 PassOwnPtr<WebCore::GraphicsContext> createGraphicsContext();
110 #if USE(CAIRO) && ENABLE(TIZEN_MOBILE_WEB_PRINT)
111 PassOwnPtr<WebCore::GraphicsContext> createGraphicsContextForPdfSurface(const String&, double, double);
114 // Paint the backing store into the given context.
115 void paint(WebCore::GraphicsContext&, const WebCore::IntPoint& destination, const WebCore::IntRect& source);
116 void paint(WebCore::GraphicsContext&, float scaleFactor, const WebCore::IntPoint& destination, const WebCore::IntRect& source);
118 bool isBackedBySharedMemory() const { return m_sharedMemory; }
120 // This creates a bitmap image that directly references the shared bitmap data.
121 // This is only safe to use when we know that the contents of the shareable bitmap won't change.
122 PassRefPtr<WebCore::Image> createImage();
125 HDC windowsContext() const;
128 // This creates a copied CGImageRef (most likely a copy-on-write) of the shareable bitmap.
129 RetainPtr<CGImageRef> makeCGImageCopy();
131 // This creates a CGImageRef 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 RetainPtr<CGImageRef> makeCGImage();
135 // This creates a BitmapImage 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 PassRefPtr<cairo_surface_t> createCairoSurface();
140 // This creates a QImage that directly references the shared bitmap data.
141 // This is only safe to use when we know that the contents of the shareable bitmap won't change.
142 QImage createQImage();
143 static void releaseSharedMemoryData(void* typelessBitmap);
147 ShareableBitmap(const WebCore::IntSize&, Flags, void*);
148 ShareableBitmap(const WebCore::IntSize&, Flags, PassRefPtr<SharedMemory>);
150 static size_t numBytesForSize(const WebCore::IntSize& size) { return size.width() * size.height() * 4; }
153 RetainPtr<CGImageRef> createCGImage(CGDataProviderRef) const;
154 static void releaseBitmapContextData(void* typelessBitmap, void* typelessData);
155 static void releaseDataProviderData(void* typelessBitmap, const void* typelessData, size_t);
159 static void releaseSurfaceData(void* typelessBitmap);
163 size_t sizeInBytes() const { return numBytesForSize(m_size); }
165 WebCore::IntSize m_size;
168 // If the shareable bitmap is backed by shared memory, this points to the shared memory object.
169 RefPtr<SharedMemory> m_sharedMemory;
171 // If the shareable bitmap is backed by fastMalloced memory, this points to the data.
175 mutable OwnPtr<HDC> m_windowsContext;
176 mutable OwnPtr<HBITMAP> m_windowsBitmap;
180 } // namespace WebKit
182 #endif // ShareableBitmap_h