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