#ifndef ShareableBitmap_h
#define ShareableBitmap_h
#include "SharedMemory.h"
#include <WebCore/IntRect.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,
SupportsExtendedColor = 1 << 1,
};
typedef unsigned Flags;
class Handle {
WTF_MAKE_NONCOPYABLE(Handle);
public:
Handle();
bool isNull() const { return m_handle.isNull(); }
void clear();
void encode(IPC::Encoder&) const;
static bool decode(IPC::Decoder&, Handle&);
private:
friend class ShareableBitmap;
mutable SharedMemory::Handle m_handle;
WebCore::IntSize m_size;
Flags m_flags;
unsigned m_bytesPerPixel;
};
static RefPtr<ShareableBitmap> create(const WebCore::IntSize&, Flags);
static RefPtr<ShareableBitmap> createShareable(const WebCore::IntSize&, Flags);
static RefPtr<ShareableBitmap> create(const WebCore::IntSize&, Flags, RefPtr<SharedMemory>);
static RefPtr<ShareableBitmap> create(const Handle&, SharedMemory::Protection = SharedMemory::Protection::ReadWrite);
bool createHandle(Handle&, SharedMemory::Protection = SharedMemory::Protection::ReadWrite) const;
~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; }
RefPtr<WebCore::Image> createImage();
#if USE(CG)
RetainPtr<CGImageRef> makeCGImageCopy();
RetainPtr<CGImageRef> makeCGImage();
#elif USE(CAIRO)
RefPtr<cairo_surface_t> createCairoSurface();
#endif
private:
ShareableBitmap(const WebCore::IntSize&, Flags, void*);
ShareableBitmap(const WebCore::IntSize&, Flags, RefPtr<SharedMemory>);
#if USE(CAIRO)
static Checked<unsigned, RecordOverflow> numBytesForSize(const WebCore::IntSize&);
static Checked<unsigned, RecordOverflow> numBytesForSize(const WebCore::IntSize& size, unsigned bytesPerPixel) { return numBytesForSize(size); }
#else
static Checked<unsigned, RecordOverflow> numBytesForSize(const WebCore::IntSize& size, unsigned bytesPerPixel) { return size.area<RecordOverflow>() * bytesPerPixel; }
#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;
#if USE(CAIRO)
size_t sizeInBytes() const { return numBytesForSize(m_size).unsafeGet(); }
#else
size_t sizeInBytes() const { return numBytesForSize(m_size, m_bytesPerPixel).unsafeGet(); }
#endif
WebCore::IntSize m_size;
Flags m_flags;
unsigned m_bytesPerPixel;
RefPtr<SharedMemory> m_sharedMemory;
void* m_data;
};
}
#endif // ShareableBitmap_h