aboutsummaryrefslogtreecommitdiffhomepage
path: root/xcode/hostapp/test.cpp
blob: aae344d0497dbfac982cf150b042551c2d70e507 (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
#include <Carbon/Carbon.h>
#include "SkCanvas.h"
#include "SkPaint.h"

extern CGImageRef SkCreateCGImageRef(const SkBitmap&);
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);
}

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);
    }

    CGColorRef color = CGColorCreateGenericRGB(1, 1, 1, 1);
    CGContextSetFillColorWithColor(cg, color);
    CGColorRelease(color);
    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);
}