ShareableBitmapCairo.cpp [plain text]
#include "config.h"
#include "ShareableBitmap.h"
#include <WebCore/BitmapImage.h>
#include <WebCore/CairoUtilities.h>
#include <WebCore/GraphicsContext.h>
#include <WebCore/PlatformContextCairo.h>
#include <WebCore/NotImplemented.h>
using namespace WebCore;
namespace WebKit {
PassOwnPtr<GraphicsContext> ShareableBitmap::createGraphicsContext()
{
RefPtr<cairo_surface_t> image = createCairoSurface();
RefPtr<cairo_t> bitmapContext = adoptRef(cairo_create(image.get()));
return adoptPtr(new GraphicsContext(bitmapContext.get()));
}
void ShareableBitmap::paint(GraphicsContext& context, const IntPoint& dstPoint, const IntRect& srcRect)
{
RefPtr<cairo_surface_t> surface = adoptRef(cairo_image_surface_create_for_data(static_cast<unsigned char*>(data()),
CAIRO_FORMAT_ARGB32,
m_size.width(), m_size.height(),
m_size.width() * 4));
FloatRect destRect(dstPoint, srcRect.size());
context.platformContext()->drawSurfaceToContext(surface.get(), destRect, srcRect, &context);
}
void ShareableBitmap::paint(GraphicsContext& context, float scaleFactor, const IntPoint& dstPoint, const IntRect& srcRect)
{
if (scaleFactor != 1) {
notImplemented();
return;
}
paint(context, dstPoint, srcRect);
}
PassRefPtr<cairo_surface_t> ShareableBitmap::createCairoSurface()
{
RefPtr<cairo_surface_t> image = adoptRef(cairo_image_surface_create_for_data(static_cast<unsigned char*>(data()),
CAIRO_FORMAT_ARGB32,
m_size.width(), m_size.height(),
m_size.width() * 4));
ref(); static cairo_user_data_key_t dataKey;
cairo_surface_set_user_data(image.get(), &dataKey, this, releaseSurfaceData);
return image.release();
}
void ShareableBitmap::releaseSurfaceData(void* typelessBitmap)
{
static_cast<ShareableBitmap*>(typelessBitmap)->deref(); }
PassRefPtr<Image> ShareableBitmap::createImage()
{
RefPtr<cairo_surface_t> surface = createCairoSurface();
if (!surface)
return 0;
return BitmapImage::create(surface.release().leakRef());
}
}