1 /*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 // EXPERIMENTAL EXPERIMENTAL EXPERIMENTAL EXPERIMENTAL
9 // DO NOT USE -- FOR INTERNAL TESTING ONLY
10
11 #include "sk_canvas.h"
12 #include "sk_data.h"
13 #include "sk_image.h"
14 #include "sk_paint.h"
15 #include "sk_shader.h"
16 #include "sk_surface.h"
17
18 extern void sk_test_c_api(sk_canvas_t*);
19
20 #define W 256
21 #define H 256
22
make_shader()23 static sk_shader_t* make_shader() {
24 sk_point_t pts[] = { { 0, 0 }, { W, H } };
25 sk_color_t colors[] = { 0xFF00FF00, 0xFF0000FF };
26 return sk_shader_new_linear_gradient(pts, colors, NULL, 2, CLAMP_SK_SHADER_TILEMODE, NULL);
27 }
28
do_draw(sk_canvas_t * canvas)29 static void do_draw(sk_canvas_t* canvas) {
30 sk_paint_t* paint = sk_paint_new();
31 sk_paint_set_antialias(paint, true);
32
33 sk_paint_set_color(paint, 0xFFFFFFFF);
34 sk_canvas_draw_paint(canvas, paint);
35
36 sk_rect_t r = { 10, 10, W - 10, H - 10 };
37
38 sk_paint_set_color(paint, 0xFFFF0000);
39 sk_canvas_draw_rect(canvas, &r, paint);
40
41 sk_shader_t* shader = make_shader();
42 sk_paint_set_shader(paint, shader);
43 sk_shader_unref(shader);
44
45 sk_canvas_draw_oval(canvas, &r, paint);
46
47 sk_paint_delete(paint);
48 }
49
sk_test_c_api(sk_canvas_t * canvas)50 void sk_test_c_api(sk_canvas_t* canvas) {
51 do_draw(canvas);
52
53 sk_imageinfo_t info = {
54 W, H, sk_colortype_get_default_8888(), OPAQUE_SK_ALPHATYPE
55 };
56 sk_surfaceprops_t surfaceProps = { UNKNOWN_SK_PIXELGEOMETRY };
57 sk_surface_t* surf = sk_surface_new_raster(&info, &surfaceProps);
58 do_draw(sk_surface_get_canvas(surf));
59
60 sk_image_t* img0 = sk_surface_new_image_snapshot(surf);
61 sk_surface_unref(surf);
62
63 sk_canvas_draw_image(canvas, img0, W + 10, 10, NULL);
64
65 sk_data_t* data = sk_image_encode(img0);
66 sk_image_unref(img0);
67
68 sk_image_t* img1 = sk_image_new_from_encoded(data, NULL);
69 sk_data_unref(data);
70
71 if (img1) {
72 sk_canvas_draw_image(canvas, img1, W/2, H/2, NULL);
73 sk_image_unref(img1);
74 }
75 }
76
77
78