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/RefCounted.h>
32#include <wtf/RefPtr.h>
33
34#if USE(CG)
35#include "ColorSpaceData.h"
36#include <wtf/RetainPtr.h>
37#endif
38
39#if USE(CAIRO)
40#include <WebCore/RefPtrCairo.h>
41#endif
42
43namespace WebCore {
44class Image;
45class GraphicsContext;
46}
47
48namespace WebKit {
49
50class ShareableBitmap : public RefCounted<ShareableBitmap> {
51public:
52 struct Configuration {
53 bool isOpaque { false };
54#if PLATFORM(COCOA)
55 ColorSpaceData colorSpace;
56#endif
57
58 void encode(IPC::Encoder&) const;
59 static bool decode(IPC::Decoder&, Configuration&);
60 };
61
62 class Handle {
63 WTF_MAKE_NONCOPYABLE(Handle);
64 public:
65 Handle();
66 Handle(Handle&&) = default;
67 Handle& operator=(Handle&&) = default;
68
69 bool isNull() const { return m_handle.isNull(); }
70
71 void clear();
72
73 void encode(IPC::Encoder&) const;
74 static bool decode(IPC::Decoder&, Handle&);
75
76 private:
77 friend class ShareableBitmap;
78
79 mutable SharedMemory::Handle m_handle;
80 WebCore::IntSize m_size;
81 Configuration m_configuration;
82 };
83
84 // Create a shareable bitmap that uses malloced memory.
85 static RefPtr<ShareableBitmap> create(const WebCore::IntSize&, Configuration);
86
87 // Create a shareable bitmap whose backing memory can be shared with another process.
88 static RefPtr<ShareableBitmap> createShareable(const WebCore::IntSize&, Configuration);
89
90 // Create a shareable bitmap from an already existing shared memory block.
91 static RefPtr<ShareableBitmap> create(const WebCore::IntSize&, Configuration, RefPtr<SharedMemory>);
92
93 // Create a shareable bitmap from a handle.
94 static RefPtr<ShareableBitmap> create(const Handle&, SharedMemory::Protection = SharedMemory::Protection::ReadWrite);
95
96 // Create a handle.
97 bool createHandle(Handle&, SharedMemory::Protection = SharedMemory::Protection::ReadWrite) const;
98
99 ~ShareableBitmap();
100
101 const WebCore::IntSize& size() const { return m_size; }
102 WebCore::IntRect bounds() const { return WebCore::IntRect(WebCore::IntPoint(), size()); }
103
104 // Create a graphics context that can be used to paint into the backing store.
105 std::unique_ptr<WebCore::GraphicsContext> createGraphicsContext();
106
107 // Paint the backing store into the given context.
108 void paint(WebCore::GraphicsContext&, const WebCore::IntPoint& destination, const WebCore::IntRect& source);
109 void paint(WebCore::GraphicsContext&, float scaleFactor, const WebCore::IntPoint& destination, const WebCore::IntRect& source);
110
111 bool isBackedBySharedMemory() const { return m_sharedMemory; }
112
113 // This creates a bitmap image that directly references the shared bitmap data.
114 // This is only safe to use when we know that the contents of the shareable bitmap won't change.
115 RefPtr<WebCore::Image> createImage();
116
117#if USE(CG)
118 // This creates a copied CGImageRef (most likely a copy-on-write) of the shareable bitmap.
119 RetainPtr<CGImageRef> makeCGImageCopy();
120
121 // This creates a CGImageRef that directly references the shared bitmap data.
122 // This is only safe to use when we know that the contents of the shareable bitmap won't change.
123 RetainPtr<CGImageRef> makeCGImage();
124#elif USE(CAIRO)
125 // This creates a BitmapImage 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 RefPtr<cairo_surface_t> createCairoSurface();
128#endif
129
130private:
131 ShareableBitmap(const WebCore::IntSize&, Configuration, void*);
132 ShareableBitmap(const WebCore::IntSize&, Configuration, RefPtr<SharedMemory>);
133
134 static Checked<unsigned, RecordOverflow> numBytesForSize(WebCore::IntSize, const ShareableBitmap::Configuration&);
135 static Checked<unsigned, RecordOverflow> calculateBytesPerRow(WebCore::IntSize, const Configuration&);
136 static unsigned calculateBytesPerPixel(const Configuration&);
137
138#if USE(CG)
139 RetainPtr<CGImageRef> createCGImage(CGDataProviderRef) const;
140 static void releaseBitmapContextData(void* typelessBitmap, void* typelessData);
141 static void releaseDataProviderData(void* typelessBitmap, const void* typelessData, size_t);
142#endif
143
144#if USE(CAIRO)
145 static void releaseSurfaceData(void* typelessBitmap);
146#endif
147
148 void* data() const;
149 size_t sizeInBytes() const { return numBytesForSize(m_size, m_configuration).unsafeGet(); }
150
151 WebCore::IntSize m_size;
152 Configuration m_configuration;
153
154 // If the shareable bitmap is backed by shared memory, this points to the shared memory object.
155 RefPtr<SharedMemory> m_sharedMemory;
156
157 // If the shareable bitmap is backed by fastMalloced memory, this points to the data.
158 void* m_data;
159};
160
161} // namespace WebKit
162
163#endif // ShareableBitmap_h
164