GraphicsContextCairoWin.cpp [plain text]
#include "config.h"
#include "GraphicsContext.h"
#include "AffineTransform.h"
#include "Path.h"
#include <cairo-win32.h>
#include "GraphicsContextPlatformPrivateCairo.h"
using namespace std;
namespace WebCore {
static cairo_t* createCairoContextWithHDC(HDC hdc, bool hasAlpha)
{
SetGraphicsMode(hdc, GM_ADVANCED);
cairo_surface_t* surface = 0;
HBITMAP bitmap = static_cast<HBITMAP>(GetCurrentObject(hdc, OBJ_BITMAP));
BITMAP info;
if (!GetObject(bitmap, sizeof(info), &info))
surface = cairo_win32_surface_create(hdc);
else {
ASSERT(info.bmBitsPixel == 32);
surface = cairo_image_surface_create_for_data((unsigned char*)info.bmBits,
CAIRO_FORMAT_ARGB32,
info.bmWidth,
info.bmHeight,
info.bmWidthBytes);
}
cairo_t* context = cairo_create(surface);
cairo_surface_destroy(surface);
return context;
}
GraphicsContext::GraphicsContext(HDC dc, bool hasAlpha)
: m_common(createGraphicsContextPrivate())
, m_data(new GraphicsContextPlatformPrivate)
{
if (dc) {
m_data->cr = createCairoContextWithHDC(dc, hasAlpha);
m_data->m_hdc = dc;
} else {
setPaintingDisabled(true);
m_data->cr = 0;
m_data->m_hdc = 0;
}
if (m_data->cr) {
setPlatformFillColor(fillColor(), fillColorSpace());
setPlatformStrokeColor(strokeColor(), strokeColorSpace());
}
}
void GraphicsContext::releaseWindowsContext(HDC hdc, const IntRect& dstRect, bool supportAlphaBlend, bool mayCreateBitmap)
{
if (!mayCreateBitmap || !hdc || !inTransparencyLayer()) {
m_data->restore();
return;
}
if (dstRect.isEmpty())
return;
HBITMAP bitmap = static_cast<HBITMAP>(GetCurrentObject(hdc, OBJ_BITMAP));
BITMAP info;
GetObject(bitmap, sizeof(info), &info);
ASSERT(info.bmBitsPixel == 32);
cairo_surface_t* image = cairo_image_surface_create_for_data((unsigned char*)info.bmBits,
CAIRO_FORMAT_ARGB32,
info.bmWidth,
info.bmHeight,
info.bmWidthBytes);
cairo_translate(m_data->cr, 0, dstRect.height());
cairo_scale(m_data->cr, dstRect.width(), -dstRect.height());
cairo_set_source_surface (m_data->cr, image, dstRect.x(), dstRect.y());
if (m_data->layers.size())
cairo_paint_with_alpha(m_data->cr, m_data->layers.last());
else
cairo_paint(m_data->cr);
cairo_surface_destroy(image);
::DeleteDC(hdc);
::DeleteObject(bitmap);
}
void GraphicsContextPlatformPrivate::syncContext(PlatformGraphicsContext* cr)
{
if (!cr)
return;
cairo_surface_t* surface = cairo_get_target(cr);
m_hdc = cairo_win32_surface_get_dc(surface);
SetGraphicsMode(m_hdc, GM_ADVANCED); }
void GraphicsContextPlatformPrivate::flush()
{
cairo_surface_t* surface = cairo_win32_surface_create(m_hdc);
cairo_surface_flush(surface);
cairo_surface_destroy(surface);
}
}