This source file includes following definitions.
- CairoSurfaceReleaseProc
- InstallCairoSurfacePixels
- LoadMatrixToContext
- LoadClipToContext
- SetMatrixClip
- LoadConfig
- Create
- Create
- CreateAndClear
- Create
- transform_
- onCreateDevice
- BeginPlatformPaint
- DrawToNativeContext
- setMatrixClip
- CreatePlatformCanvas
- Allocate
#include "skia/ext/bitmap_platform_device_cairo.h"
#include "skia/ext/platform_canvas.h"
#if defined(OS_OPENBSD)
#include <cairo.h>
#else
#include <cairo/cairo.h>
#endif
namespace skia {
namespace {
void CairoSurfaceReleaseProc(void*, void* context) {
SkASSERT(context);
cairo_surface_destroy(static_cast<cairo_surface_t*>(context));
}
bool InstallCairoSurfacePixels(SkBitmap* dst,
cairo_surface_t* surface,
bool is_opaque) {
SkASSERT(dst);
if (!surface) {
return false;
}
SkImageInfo info
= SkImageInfo::MakeN32Premul(cairo_image_surface_get_width(surface),
cairo_image_surface_get_height(surface));
return dst->installPixels(info,
cairo_image_surface_get_data(surface),
cairo_image_surface_get_stride(surface),
&CairoSurfaceReleaseProc,
static_cast<void*>(surface));
}
void LoadMatrixToContext(cairo_t* context, const SkMatrix& matrix) {
cairo_matrix_t cairo_matrix;
cairo_matrix_init(&cairo_matrix,
SkScalarToFloat(matrix.getScaleX()),
SkScalarToFloat(matrix.getSkewY()),
SkScalarToFloat(matrix.getSkewX()),
SkScalarToFloat(matrix.getScaleY()),
SkScalarToFloat(matrix.getTranslateX()),
SkScalarToFloat(matrix.getTranslateY()));
cairo_set_matrix(context, &cairo_matrix);
}
void LoadClipToContext(cairo_t* context, const SkRegion& clip) {
cairo_reset_clip(context);
SkIRect bounding = clip.getBounds();
cairo_rectangle(context, bounding.fLeft, bounding.fTop,
bounding.fRight - bounding.fLeft,
bounding.fBottom - bounding.fTop);
cairo_clip(context);
}
}
void BitmapPlatformDevice::SetMatrixClip(
const SkMatrix& transform,
const SkRegion& region) {
transform_ = transform;
clip_region_ = region;
config_dirty_ = true;
}
void BitmapPlatformDevice::LoadConfig() {
if (!config_dirty_ || !cairo_)
return;
config_dirty_ = false;
cairo_matrix_t cairo_matrix;
cairo_matrix_init_identity(&cairo_matrix);
cairo_set_matrix(cairo_, &cairo_matrix);
LoadClipToContext(cairo_, clip_region_);
LoadMatrixToContext(cairo_, transform_);
}
BitmapPlatformDevice* BitmapPlatformDevice::Create(int width, int height,
bool is_opaque,
cairo_surface_t* surface) {
if (cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
cairo_surface_destroy(surface);
return NULL;
}
cairo_t* cairo = cairo_create(surface);
SkBitmap bitmap;
if (!InstallCairoSurfacePixels(&bitmap, surface, is_opaque)) {
cairo_destroy(cairo);
return NULL;
}
return new BitmapPlatformDevice(bitmap, cairo);
}
BitmapPlatformDevice* BitmapPlatformDevice::Create(int width, int height,
bool is_opaque) {
cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
width, height);
BitmapPlatformDevice* device = Create(width, height, is_opaque, surface);
#ifndef NDEBUG
if (device && is_opaque)
device->eraseColor(SkColorSetARGB(255, 0, 255, 128));
#endif
return device;
}
BitmapPlatformDevice* BitmapPlatformDevice::CreateAndClear(int width,
int height,
bool is_opaque) {
return Create(width, height, is_opaque);
}
BitmapPlatformDevice* BitmapPlatformDevice::Create(int width, int height,
bool is_opaque,
uint8_t* data) {
cairo_surface_t* surface = cairo_image_surface_create_for_data(
data, CAIRO_FORMAT_ARGB32, width, height,
cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width));
return Create(width, height, is_opaque, surface);
}
BitmapPlatformDevice::BitmapPlatformDevice(
const SkBitmap& bitmap,
cairo_t* cairo)
: SkBitmapDevice(bitmap),
cairo_(cairo),
config_dirty_(true),
transform_(SkMatrix::I()) {
SetPlatformDevice(this, this);
}
BitmapPlatformDevice::~BitmapPlatformDevice() {
cairo_destroy(cairo_);
}
SkBaseDevice* BitmapPlatformDevice::onCreateDevice(const SkImageInfo& info,
Usage ) {
SkASSERT(info.colorType() == kPMColor_SkColorType);
return BitmapPlatformDevice::Create(info.width(), info.height(),
info.isOpaque());
}
cairo_t* BitmapPlatformDevice::BeginPlatformPaint() {
LoadConfig();
cairo_surface_t* surface = cairo_get_target(cairo_);
cairo_surface_flush(surface);
cairo_surface_mark_dirty(surface);
return cairo_;
}
void BitmapPlatformDevice::DrawToNativeContext(
PlatformSurface surface, int x, int y, const PlatformRect* src_rect) {
SkASSERT(false);
}
void BitmapPlatformDevice::setMatrixClip(const SkMatrix& transform,
const SkRegion& region,
const SkClipStack&) {
SetMatrixClip(transform, region);
}
SkCanvas* CreatePlatformCanvas(int width, int height, bool is_opaque,
uint8_t* data, OnFailureType failureType) {
skia::RefPtr<SkBaseDevice> dev = skia::AdoptRef(
BitmapPlatformDevice::Create(width, height, is_opaque, data));
return CreateCanvas(dev, failureType);
}
PlatformBitmap::~PlatformBitmap() {
cairo_destroy(surface_);
}
bool PlatformBitmap::Allocate(int width, int height, bool is_opaque) {
int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
cairo_surface_t* surf = cairo_image_surface_create(
CAIRO_FORMAT_ARGB32,
width,
height);
if (cairo_surface_status(surf) != CAIRO_STATUS_SUCCESS) {
cairo_surface_destroy(surf);
return false;
}
return InstallCairoSurfacePixels(&bitmap_, surf, is_opaque);
}
}