aboutsummaryrefslogtreecommitdiffstats
path: root/xcode/hostapp/test.cpp
blob: 4f66a826bf37a3c6b23b1ae9a6cbc0ee436a6024 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <Carbon/Carbon.h>
#include "SkCGUtils.h"
#include "SkCanvas.h"
#include "SkPaint.h"

extern "C" void SkiaDraw(CGContextRef cg, CGRect bounds);

static void sampleDraw(SkCanvas* canvas) {
    canvas->drawColor(0xFF0000FF);
    
    SkPaint paint;
    paint.setAntiAlias(true);

    canvas->drawCircle(SkIntToScalar(100), SkIntToScalar(100),
                       SkIntToScalar(90), paint);
    
    const char text[] = "fry42";
    const size_t len = strlen(text);

    paint.setColor(SK_ColorWHITE);
    paint.setTextSize(SkIntToScalar(50));
    canvas->drawText(text, len, SkIntToScalar(100), SkIntToScalar(50), paint);
    paint.setTextAlign(SkPaint::kCenter_Align);
    canvas->drawText(text, len, SkIntToScalar(100), SkIntToScalar(100), paint);
    paint.setTextAlign(SkPaint::kRight_Align);
    canvas->drawText(text, len, SkIntToScalar(100), SkIntToScalar(150), paint);
}

static CGImageRef gImage;

void SkiaDraw(CGContextRef cg, CGRect bounds) {
    if (NULL == gImage) {
        SkBitmap bitmap;
        bitmap.setConfig(SkBitmap::kARGB_8888_Config, 640, 480);
        bitmap.allocPixels();
        
        SkCanvas canvas(bitmap);
        sampleDraw(&canvas);
        
        gImage = SkCreateCGImageRef(bitmap);
    }

	float components[] = { 1, 1, 1, 1 };

	CGColorSpaceRef colorspace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
	CGColorRef color = CGColorCreate(colorspace, components);

    CGContextSetFillColorWithColor(cg, color);
    CGColorRelease(color);
	CGColorSpaceRelease(colorspace);

    CGContextFillRect(cg, bounds);

    CGRect r = CGRectMake(0, 0, 640, 480);
    
    CGContextSaveGState(cg);
    CGContextTranslateCTM(cg, 0, r.size.height);
    CGContextScaleCTM(cg, 1, -1);
    
    CGContextDrawImage(cg, r, gImage);
    
    CGContextRestoreGState(cg);
}