#ifndef ShareableBitmap_h
#define ShareableBitmap_h
#include "SharedMemory.h"
#include <WebCore/IntRect.h>
#include <wtf/PassRefPtr.h>
#include <wtf/RefCounted.h>
#include <wtf/RefPtr.h>
#if USE(CG)
#include <wtf/RetainPtr.h>
#endif
#if USE(CAIRO)
#include <WebCore/RefPtrCairo.h>
#endif
namespace WebCore {
class Image;
class GraphicsContext;
}
namespace WebKit {
class ShareableBitmap : public RefCounted<ShareableBitmap> {
public:
enum Flag {
NoFlags = 0,
SupportsAlpha = 1 << 0,
};
typedef unsigned Flags;
class Handle {
WTF_MAKE_NONCOPYABLE(Handle);
public:
Handle();
bool isNull() const { return m_handle.isNull(); }
void clear();
void encode(IPC::ArgumentEncoder&) const;
static bool decode(IPC::ArgumentDecoder&, Handle&);
private:
friend class ShareableBitmap;
mutable SharedMemory::Handle m_handle;
WebCore::IntSize m_size;
Flags m_flags;
};
static PassRefPtr<ShareableBitmap> create(const WebCore::IntSize&, Flags);
static PassRefPtr<ShareableBitmap> createShareable(const WebCore::IntSize&, Flags);
static PassRefPtr<ShareableBitmap> create(const WebCore::IntSize&, Flags, PassRefPtr<SharedMemory>);
static PassRefPtr<ShareableBitmap> create(const Handle&, SharedMemory::Protection = SharedMemory::Protection::ReadWrite);
bool createHandle(Handle&, SharedMemory::Protection = SharedMemory::Protection::ReadWrite);
~ShareableBitmap();
const WebCore::IntSize& size() const { return m_size; }
WebCore::IntRect bounds() const { return WebCore::IntRect(WebCore::IntPoint(), size()); }
std::unique_ptr<WebCore::GraphicsContext> createGraphicsContext();
void paint(WebCore::GraphicsContext&, const WebCore::IntPoint& destination, const WebCore::IntRect& source);
void paint(WebCore::GraphicsContext&, float scaleFactor, const WebCore::IntPoint& destination, const WebCore::IntRect& source);
bool isBackedBySharedMemory() const { return m_sharedMemory; }
PassRefPtr<WebCore::Image> createImage();
#if USE(CG)
RetainPtr<CGImageRef> makeCGImageCopy();
RetainPtr<CGImageRef> makeCGImage();
#elif USE(CAIRO)
PassRefPtr<cairo_surface_t> createCairoSurface();
#endif
private:
ShareableBitmap(const WebCore::IntSize&, Flags, void*);
ShareableBitmap(const WebCore::IntSize&, Flags, PassRefPtr<SharedMemory>);
#if USE(CAIRO)
static size_t numBytesForSize(const WebCore::IntSize&);
#else
static size_t numBytesForSize(const WebCore::IntSize& size) { return size.width() * size.height() * 4; }
#endif
#if USE(CG)
RetainPtr<CGImageRef> createCGImage(CGDataProviderRef) const;
static void releaseBitmapContextData(void* typelessBitmap, void* typelessData);
static void releaseDataProviderData(void* typelessBitmap, const void* typelessData, size_t);
#endif
#if USE(CAIRO)
static void releaseSurfaceData(void* typelessBitmap);
#endif
void* data() const;
size_t sizeInBytes() const { return numBytesForSize(m_size); }
WebCore::IntSize m_size;
Flags m_flags;
RefPtr<SharedMemory> m_sharedMemory;
void* m_data;
};
}
#endif // ShareableBitmap_h