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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/c/sk_canvas.h" |
| 12 | #include "include/c/sk_data.h" |
| 13 | #include "include/c/sk_image.h" |
| 14 | #include "include/c/sk_imageinfo.h" |
| 15 | #include "include/c/sk_paint.h" |
| 16 | #include "include/c/sk_shader.h" |
| 17 | #include "include/c/sk_surface.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 18 | #include "include/c/sk_types.h" |
| 19 | |
| 20 | #include <stdbool.h> |
| 21 | #include <stddef.h> |
reed | afa278e | 2014-11-24 19:11:48 -0800 | [diff] [blame] | 22 | |
| 23 | extern void sk_test_c_api(sk_canvas_t*); |
| 24 | |
| 25 | #define W 256 |
| 26 | #define H 256 |
| 27 | |
| 28 | static sk_shader_t* make_shader() { |
| 29 | sk_point_t pts[] = { { 0, 0 }, { W, H } }; |
| 30 | sk_color_t colors[] = { 0xFF00FF00, 0xFF0000FF }; |
| 31 | return sk_shader_new_linear_gradient(pts, colors, NULL, 2, CLAMP_SK_SHADER_TILEMODE, NULL); |
| 32 | } |
| 33 | |
reed | b2a5d7e | 2014-12-25 14:16:21 -0800 | [diff] [blame] | 34 | static void do_draw(sk_canvas_t* canvas) { |
reed | afa278e | 2014-11-24 19:11:48 -0800 | [diff] [blame] | 35 | sk_paint_t* paint = sk_paint_new(); |
| 36 | sk_paint_set_antialias(paint, true); |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 37 | |
reed | afa278e | 2014-11-24 19:11:48 -0800 | [diff] [blame] | 38 | sk_paint_set_color(paint, 0xFFFFFFFF); |
| 39 | sk_canvas_draw_paint(canvas, paint); |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 40 | |
reed | afa278e | 2014-11-24 19:11:48 -0800 | [diff] [blame] | 41 | sk_rect_t r = { 10, 10, W - 10, H - 10 }; |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 42 | |
reed | afa278e | 2014-11-24 19:11:48 -0800 | [diff] [blame] | 43 | sk_paint_set_color(paint, 0xFFFF0000); |
| 44 | sk_canvas_draw_rect(canvas, &r, paint); |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 45 | |
reed | afa278e | 2014-11-24 19:11:48 -0800 | [diff] [blame] | 46 | sk_shader_t* shader = make_shader(); |
| 47 | sk_paint_set_shader(paint, shader); |
| 48 | sk_shader_unref(shader); |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 49 | |
reed | afa278e | 2014-11-24 19:11:48 -0800 | [diff] [blame] | 50 | sk_canvas_draw_oval(canvas, &r, paint); |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 51 | |
reed | afa278e | 2014-11-24 19:11:48 -0800 | [diff] [blame] | 52 | sk_paint_delete(paint); |
| 53 | } |
| 54 | |
reed | b2a5d7e | 2014-12-25 14:16:21 -0800 | [diff] [blame] | 55 | void sk_test_c_api(sk_canvas_t* canvas) { |
| 56 | do_draw(canvas); |
| 57 | |
Mike Reed | 66c505f | 2018-09-28 11:27:57 -0400 | [diff] [blame] | 58 | sk_imageinfo_t* info = sk_imageinfo_new(W, H, RGBA_8888_SK_COLORTYPE, OPAQUE_SK_ALPHATYPE, |
| 59 | NULL); |
robertphillips | 702edbd | 2015-06-23 06:26:08 -0700 | [diff] [blame] | 60 | sk_surfaceprops_t surfaceProps = { UNKNOWN_SK_PIXELGEOMETRY }; |
Mike Reed | 66c505f | 2018-09-28 11:27:57 -0400 | [diff] [blame] | 61 | sk_surface_t* surf = sk_surface_new_raster(info, &surfaceProps); |
| 62 | sk_imageinfo_delete(info); |
reed | b2a5d7e | 2014-12-25 14:16:21 -0800 | [diff] [blame] | 63 | do_draw(sk_surface_get_canvas(surf)); |
| 64 | |
| 65 | sk_image_t* img0 = sk_surface_new_image_snapshot(surf); |
| 66 | sk_surface_unref(surf); |
| 67 | |
Mike Reed | 18aeb57 | 2021-01-19 17:58:25 -0500 | [diff] [blame] | 68 | sk_canvas_draw_image(canvas, img0, W + 10, 10, NULL, NULL); |
reed | b2a5d7e | 2014-12-25 14:16:21 -0800 | [diff] [blame] | 69 | |
| 70 | sk_data_t* data = sk_image_encode(img0); |
| 71 | sk_image_unref(img0); |
| 72 | |
Mike Reed | 564d49e | 2020-07-28 12:52:31 -0400 | [diff] [blame] | 73 | sk_image_t* img1 = sk_image_new_from_encoded(data); |
reed | b2a5d7e | 2014-12-25 14:16:21 -0800 | [diff] [blame] | 74 | sk_data_unref(data); |
| 75 | |
reed | 2906d4a | 2014-12-25 20:41:13 -0800 | [diff] [blame] | 76 | if (img1) { |
Mike Reed | 18aeb57 | 2021-01-19 17:58:25 -0500 | [diff] [blame] | 77 | sk_canvas_draw_image(canvas, img1, W/2, H/2, NULL, NULL); |
reed | 2906d4a | 2014-12-25 20:41:13 -0800 | [diff] [blame] | 78 | sk_image_unref(img1); |
| 79 | } |
reed | b2a5d7e | 2014-12-25 14:16:21 -0800 | [diff] [blame] | 80 | } |
| 81 | |
reed | afa278e | 2014-11-24 19:11:48 -0800 | [diff] [blame] | 82 | |