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 // 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);
114 bool isBackedBySharedMemory() const { return m_sharedMemory; }
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();
121 HDC windowsContext() const;
124 // This creates a copied CGImageRef (most likely a copy-on-write) of the shareable bitmap.
125 RetainPtr<CGImageRef> makeCGImageCopy();
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();
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();
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();
141 ShareableBitmap(const WebCore::IntSize&, Flags, void*);
142 ShareableBitmap(const WebCore::IntSize&, Flags, PassRefPtr<SharedMemory>);
144 static size_t numBytesForSize(const WebCore::IntSize& size) { return size.width() * size.height() * 4; }
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);
153 static void releaseSurfaceData(void* typelessBitmap);
157 size_t sizeInBytes() const { return numBytesForSize(m_size); }
159 WebCore::IntSize m_size;
162 // If the shareable bitmap is backed by shared memory, this points to the shared memory object.
163 RefPtr<SharedMemory> m_sharedMemory;
165 // If the shareable bitmap is backed by fastMalloced memory, this points to the data.
169 mutable OwnPtr<HDC> m_windowsContext;
170 mutable OwnPtr<HBITMAP> m_windowsBitmap;
174 } // namespace WebKit
176 #endif // ShareableBitmap_h