GraphicsContextCairoWin.cpp [plain text]
#include "config.h"
#include "GraphicsContext.h"
#include "AffineTransform.h"
#include "DIBPixelData.h"
#include "Path.h"
#include <cairo-win32.h>
#include "GraphicsContextPlatformPrivateCairo.h"
using namespace std;
namespace WebCore {
#if PLATFORM(WIN)
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)
{
platformInit(dc, hasAlpha);
}
void GraphicsContext::platformInit(HDC dc, bool hasAlpha)
{
if (!dc)
return;
cairo_t* cr = createCairoContextWithHDC(dc, hasAlpha);
m_data = new GraphicsContextPlatformPrivateToplevel(new PlatformContextCairo(cr));
m_data->m_hdc = dc;
if (platformContext()->cr()) {
setPlatformFillColor(fillColor());
setPlatformStrokeColor(strokeColor());
}
if (cr)
cairo_destroy(cr);
}
#endif
static void setRGBABitmapAlpha(unsigned char* bytes, size_t length, unsigned char level)
{
for (size_t i = 0; i < length; i += 4)
bytes[i + 3] = level;
}
static void drawBitmapToContext(GraphicsContextPlatformPrivate* context, cairo_t* cr, const DIBPixelData& pixelData, const IntSize& translate)
{
cairo_surface_t* surface = cairo_image_surface_create_for_data(pixelData.buffer(),
CAIRO_FORMAT_ARGB32,
pixelData.size().width(),
pixelData.size().height(),
pixelData.bytesPerRow());
cairo_save(cr);
cairo_translate(cr, static_cast<double>(translate.width()), static_cast<double>(translate.height()));
cairo_scale(cr, 1, -1);
cairo_set_source_surface(cr, surface, 0, 0);
if (context->layers.size())
cairo_paint_with_alpha(cr, context->layers.last());
else
cairo_paint(cr);
cairo_surface_destroy(surface);
cairo_restore(cr);
}
void GraphicsContext::releaseWindowsContext(HDC hdc, const IntRect& dstRect, bool supportAlphaBlend, bool mayCreateBitmap)
{
bool createdBitmap = mayCreateBitmap && (!m_data->m_hdc || isInTransparencyLayer());
if (!hdc || !createdBitmap) {
m_data->restore();
return;
}
if (dstRect.isEmpty())
return;
auto bitmap = adoptGDIObject(static_cast<HBITMAP>(::GetCurrentObject(hdc, OBJ_BITMAP)));
DIBPixelData pixelData(bitmap.get());
ASSERT(pixelData.bitsPerPixel() == 32);
unsigned char* bytes = reinterpret_cast<unsigned char*>(pixelData.buffer());
if (!supportAlphaBlend)
setRGBABitmapAlpha(bytes, pixelData.size().height() * pixelData.bytesPerRow(), 255);
drawBitmapToContext(m_data, platformContext()->cr(), pixelData, IntSize(dstRect.x(), dstRect.height() + dstRect.y()));
::DeleteDC(hdc);
}
#if PLATFORM(WIN)
void GraphicsContext::drawWindowsBitmap(WindowsBitmap* bitmap, const IntPoint& point)
{
drawBitmapToContext(m_data, platformContext()->cr(), bitmap->windowsDIB(), IntSize(point.x(), bitmap->size().height() + point.y()));
}
void GraphicsContextPlatformPrivate::syncContext(cairo_t* 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);
}
#endif
}