reed | afa278e | 2014-11-24 19:11:48 -0800 | [diff] [blame] | 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" |
reed | b2a5d7e | 2014-12-25 14:16:21 -0800 | [diff] [blame] | 12 | #include "sk_data.h" |
| 13 | #include "sk_image.h" |
reed | afa278e | 2014-11-24 19:11:48 -0800 | [diff] [blame] | 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 | |
| 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 | |
reed | b2a5d7e | 2014-12-25 14:16:21 -0800 | [diff] [blame] | 29 | static void do_draw(sk_canvas_t* canvas) { |
reed | afa278e | 2014-11-24 19:11:48 -0800 | [diff] [blame] | 30 | sk_paint_t* paint = sk_paint_new(); |
| 31 | sk_paint_set_antialias(paint, true); |
reed | b2a5d7e | 2014-12-25 14:16:21 -0800 | [diff] [blame] | 32 | |
reed | afa278e | 2014-11-24 19:11:48 -0800 | [diff] [blame] | 33 | sk_paint_set_color(paint, 0xFFFFFFFF); |
| 34 | sk_canvas_draw_paint(canvas, paint); |
reed | b2a5d7e | 2014-12-25 14:16:21 -0800 | [diff] [blame] | 35 | |
reed | afa278e | 2014-11-24 19:11:48 -0800 | [diff] [blame] | 36 | sk_rect_t r = { 10, 10, W - 10, H - 10 }; |
reed | b2a5d7e | 2014-12-25 14:16:21 -0800 | [diff] [blame] | 37 | |
reed | afa278e | 2014-11-24 19:11:48 -0800 | [diff] [blame] | 38 | sk_paint_set_color(paint, 0xFFFF0000); |
| 39 | sk_canvas_draw_rect(canvas, &r, paint); |
reed | b2a5d7e | 2014-12-25 14:16:21 -0800 | [diff] [blame] | 40 | |
reed | afa278e | 2014-11-24 19:11:48 -0800 | [diff] [blame] | 41 | sk_shader_t* shader = make_shader(); |
| 42 | sk_paint_set_shader(paint, shader); |
| 43 | sk_shader_unref(shader); |
reed | b2a5d7e | 2014-12-25 14:16:21 -0800 | [diff] [blame] | 44 | |
reed | afa278e | 2014-11-24 19:11:48 -0800 | [diff] [blame] | 45 | sk_canvas_draw_oval(canvas, &r, paint); |
reed | b2a5d7e | 2014-12-25 14:16:21 -0800 | [diff] [blame] | 46 | |
reed | afa278e | 2014-11-24 19:11:48 -0800 | [diff] [blame] | 47 | sk_paint_delete(paint); |
| 48 | } |
| 49 | |
reed | b2a5d7e | 2014-12-25 14:16:21 -0800 | [diff] [blame] | 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 | }; |
robertphillips | 702edbd | 2015-06-23 06:26:08 -0700 | [diff] [blame] | 56 | sk_surfaceprops_t surfaceProps = { UNKNOWN_SK_PIXELGEOMETRY }; |
| 57 | sk_surface_t* surf = sk_surface_new_raster(&info, &surfaceProps); |
reed | b2a5d7e | 2014-12-25 14:16:21 -0800 | [diff] [blame] | 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 | |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 68 | sk_image_t* img1 = sk_image_new_from_encoded(data, NULL); |
reed | b2a5d7e | 2014-12-25 14:16:21 -0800 | [diff] [blame] | 69 | sk_data_unref(data); |
| 70 | |
reed | 2906d4a | 2014-12-25 20:41:13 -0800 | [diff] [blame] | 71 | if (img1) { |
| 72 | sk_canvas_draw_image(canvas, img1, W/2, H/2, NULL); |
| 73 | sk_image_unref(img1); |
| 74 | } |
reed | b2a5d7e | 2014-12-25 14:16:21 -0800 | [diff] [blame] | 75 | } |
| 76 | |
reed | afa278e | 2014-11-24 19:11:48 -0800 | [diff] [blame] | 77 | |