This source file includes following definitions.
- GetBitmapContext
- BeginPlatformPaint
- EndPlatformPaint
- InitializeCGContext
- LoadPathToCGContext
- LoadTransformToCGContext
- LoadClippingRegionToCGContext
#include "skia/ext/platform_device.h"
#include "skia/ext/bitmap_platform_device.h"
#import <ApplicationServices/ApplicationServices.h>
#include "skia/ext/skia_utils_mac.h"
#include "third_party/skia/include/core/SkMatrix.h"
#include "third_party/skia/include/core/SkPath.h"
#include "third_party/skia/include/core/SkTypes.h"
#include "third_party/skia/include/core/SkUtils.h"
namespace skia {
CGContextRef GetBitmapContext(SkBaseDevice* device) {
PlatformDevice* platform_device = GetPlatformDevice(device);
if (platform_device)
return platform_device->GetBitmapContext();
return NULL;
}
CGContextRef PlatformDevice::BeginPlatformPaint() {
return GetBitmapContext();
}
void PlatformDevice::EndPlatformPaint() {
}
void PlatformDevice::InitializeCGContext(CGContextRef context) {
}
void PlatformDevice::LoadPathToCGContext(CGContextRef context,
const SkPath& path) {
CGContextBeginPath(context);
SkPoint points[4] = { {0, 0}, {0, 0}, {0, 0}, {0, 0} };
SkPath::Iter iter(path, false);
for (SkPath::Verb verb = iter.next(points); verb != SkPath::kDone_Verb;
verb = iter.next(points)) {
switch (verb) {
case SkPath::kMove_Verb: {
CGContextMoveToPoint(context, points[0].fX, points[0].fY);
break;
}
case SkPath::kLine_Verb: {
CGContextAddLineToPoint(context, points[1].fX, points[1].fY);
break;
}
case SkPath::kQuad_Verb: {
CGContextAddQuadCurveToPoint(context, points[1].fX, points[1].fY,
points[2].fX, points[2].fY);
break;
}
case SkPath::kCubic_Verb: {
CGContextAddCurveToPoint(context, points[1].fX, points[1].fY,
points[2].fX, points[2].fY,
points[3].fX, points[3].fY);
break;
}
case SkPath::kClose_Verb: {
break;
}
case SkPath::kDone_Verb:
default: {
SkASSERT(false);
break;
}
}
}
CGContextClosePath(context);
}
void PlatformDevice::LoadTransformToCGContext(CGContextRef context,
const SkMatrix& matrix) {
CGAffineTransform orig_cg_matrix = CGContextGetCTM(context);
CGAffineTransform orig_cg_matrix_inv = CGAffineTransformInvert(
orig_cg_matrix);
CGContextConcatCTM(context, orig_cg_matrix_inv);
SkASSERT(CGAffineTransformIsIdentity(CGContextGetCTM(context)));
SkMatrix transformed_matrix = matrix;
SkScalar sy = matrix.getScaleY() * (SkScalar)-1;
transformed_matrix.setScaleY(sy);
size_t height = CGBitmapContextGetHeight(context);
SkScalar ty = -matrix.getTranslateY();
transformed_matrix.setTranslateY(ty + (SkScalar)height);
CGAffineTransform cg_matrix = gfx::SkMatrixToCGAffineTransform(
transformed_matrix);
CGContextConcatCTM(context, cg_matrix);
}
void PlatformDevice::LoadClippingRegionToCGContext(
CGContextRef context,
const SkRegion& region,
const SkMatrix& transformation) {
if (region.isEmpty()) {
SkRect rect;
rect.setEmpty();
CGContextClipToRect(context, gfx::SkRectToCGRect(rect));
} else if (region.isRect()) {
SkMatrix t;
bool did_invert = transformation.invert(&t);
if (!did_invert)
t.reset();
SkRect rect;
rect.set(region.getBounds());
t.mapRect(&rect);
SkIRect irect;
rect.round(&irect);
CGContextClipToRect(context, gfx::SkIRectToCGRect(irect));
} else {
SkPath path;
region.getBoundaryPath(&path);
path.transform(transformation);
SkASSERT(false);
}
}
}