1 #include <Carbon/Carbon.h>
2 #include "SkCGUtils.h"
3 #include "SkCanvas.h"
4 #include "SkPaint.h"
5
6 extern "C" void SkiaDraw(CGContextRef cg, CGRect bounds);
7
sampleDraw(SkCanvas * canvas)8 static void sampleDraw(SkCanvas* canvas) {
9 canvas->drawColor(0xFF0000FF);
10
11 SkPaint paint;
12 paint.setAntiAlias(true);
13
14 canvas->drawCircle(SkIntToScalar(100), SkIntToScalar(100),
15 SkIntToScalar(90), paint);
16
17 const char text[] = "fry42";
18 const size_t len = strlen(text);
19
20 paint.setColor(SK_ColorWHITE);
21 paint.setTextSize(SkIntToScalar(50));
22 canvas->drawText(text, len, SkIntToScalar(100), SkIntToScalar(50), paint);
23 paint.setTextAlign(SkPaint::kCenter_Align);
24 canvas->drawText(text, len, SkIntToScalar(100), SkIntToScalar(100), paint);
25 paint.setTextAlign(SkPaint::kRight_Align);
26 canvas->drawText(text, len, SkIntToScalar(100), SkIntToScalar(150), paint);
27 }
28
29 static CGImageRef gImage;
30
SkiaDraw(CGContextRef cg,CGRect bounds)31 void SkiaDraw(CGContextRef cg, CGRect bounds) {
32 if (NULL == gImage) {
33 SkBitmap bitmap;
34 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 640, 480);
35 bitmap.allocPixels();
36
37 SkCanvas canvas(bitmap);
38 sampleDraw(&canvas);
39
40 gImage = SkCreateCGImageRef(bitmap);
41 }
42
43 float components[] = { 1, 1, 1, 1 };
44
45 CGColorSpaceRef colorspace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
46 CGColorRef color = CGColorCreate(colorspace, components);
47
48 CGContextSetFillColorWithColor(cg, color);
49 CGColorRelease(color);
50 CGColorSpaceRelease(colorspace);
51
52 CGContextFillRect(cg, bounds);
53
54 CGRect r = CGRectMake(0, 0, 640, 480);
55
56 CGContextSaveGState(cg);
57 CGContextTranslateCTM(cg, 0, r.size.height);
58 CGContextScaleCTM(cg, 1, -1);
59
60 CGContextDrawImage(cg, r, gImage);
61
62 CGContextRestoreGState(cg);
63 }
64
65
66