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