blob: cc03d89233a4c220b7b284ad2c355a53194585fe [file] [log] [blame]
reedafa278e2014-11-24 19:11:48 -08001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -050011#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 Wagner7fde8e12019-05-01 17:28:53 -040018#include "include/c/sk_types.h"
19
20#include <stdbool.h>
21#include <stddef.h>
reedafa278e2014-11-24 19:11:48 -080022
23extern void sk_test_c_api(sk_canvas_t*);
24
25#define W 256
26#define H 256
27
28static 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
reedb2a5d7e2014-12-25 14:16:21 -080034static void do_draw(sk_canvas_t* canvas) {
reedafa278e2014-11-24 19:11:48 -080035 sk_paint_t* paint = sk_paint_new();
36 sk_paint_set_antialias(paint, true);
Ben Wagner63fd7602017-10-09 15:45:33 -040037
reedafa278e2014-11-24 19:11:48 -080038 sk_paint_set_color(paint, 0xFFFFFFFF);
39 sk_canvas_draw_paint(canvas, paint);
Ben Wagner63fd7602017-10-09 15:45:33 -040040
reedafa278e2014-11-24 19:11:48 -080041 sk_rect_t r = { 10, 10, W - 10, H - 10 };
Ben Wagner63fd7602017-10-09 15:45:33 -040042
reedafa278e2014-11-24 19:11:48 -080043 sk_paint_set_color(paint, 0xFFFF0000);
44 sk_canvas_draw_rect(canvas, &r, paint);
Ben Wagner63fd7602017-10-09 15:45:33 -040045
reedafa278e2014-11-24 19:11:48 -080046 sk_shader_t* shader = make_shader();
47 sk_paint_set_shader(paint, shader);
48 sk_shader_unref(shader);
Ben Wagner63fd7602017-10-09 15:45:33 -040049
reedafa278e2014-11-24 19:11:48 -080050 sk_canvas_draw_oval(canvas, &r, paint);
Ben Wagner63fd7602017-10-09 15:45:33 -040051
reedafa278e2014-11-24 19:11:48 -080052 sk_paint_delete(paint);
53}
54
reedb2a5d7e2014-12-25 14:16:21 -080055void sk_test_c_api(sk_canvas_t* canvas) {
56 do_draw(canvas);
57
Mike Reed66c505f2018-09-28 11:27:57 -040058 sk_imageinfo_t* info = sk_imageinfo_new(W, H, RGBA_8888_SK_COLORTYPE, OPAQUE_SK_ALPHATYPE,
59 NULL);
robertphillips702edbd2015-06-23 06:26:08 -070060 sk_surfaceprops_t surfaceProps = { UNKNOWN_SK_PIXELGEOMETRY };
Mike Reed66c505f2018-09-28 11:27:57 -040061 sk_surface_t* surf = sk_surface_new_raster(info, &surfaceProps);
62 sk_imageinfo_delete(info);
reedb2a5d7e2014-12-25 14:16:21 -080063 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 Reed18aeb572021-01-19 17:58:25 -050068 sk_canvas_draw_image(canvas, img0, W + 10, 10, NULL, NULL);
reedb2a5d7e2014-12-25 14:16:21 -080069
70 sk_data_t* data = sk_image_encode(img0);
71 sk_image_unref(img0);
72
Mike Reed564d49e2020-07-28 12:52:31 -040073 sk_image_t* img1 = sk_image_new_from_encoded(data);
reedb2a5d7e2014-12-25 14:16:21 -080074 sk_data_unref(data);
75
reed2906d4a2014-12-25 20:41:13 -080076 if (img1) {
Mike Reed18aeb572021-01-19 17:58:25 -050077 sk_canvas_draw_image(canvas, img1, W/2, H/2, NULL, NULL);
reed2906d4a2014-12-25 20:41:13 -080078 sk_image_unref(img1);
79 }
reedb2a5d7e2014-12-25 14:16:21 -080080}
81
reedafa278e2014-11-24 19:11:48 -080082