blob: 9c0868d48a20880b7faaf13c16bc88d9f36b96a1 [file] [log] [blame]
reede3323962014-10-24 11:16:19 -07001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/c/sk_canvas.h"
9#include "include/c/sk_imageinfo.h"
10#include "include/c/sk_paint.h"
11#include "include/c/sk_shader.h"
12#include "include/c/sk_surface.h"
13#include "include/c/sk_types.h"
14#include "tests/Test.h"
Ben Wagnereed61282018-04-17 14:14:51 -040015
Ben Wagner9707a7e2019-05-06 17:17:19 -040016#include <stddef.h>
Ben Wagnereed61282018-04-17 14:14:51 -040017#include <stdint.h>
reed73c25012014-11-17 06:15:42 -080018
halcanary65cc3e42015-08-12 07:37:34 -070019static void shader_test(skiatest::Reporter* reporter) {
Mike Reed66c505f2018-09-28 11:27:57 -040020 sk_imageinfo_t* info = sk_imageinfo_new(64, 64, RGBA_8888_SK_COLORTYPE, PREMUL_SK_ALPHATYPE,
21 NULL);
22 sk_surface_t* surface = sk_surface_new_raster(info, nullptr);
halcanary65cc3e42015-08-12 07:37:34 -070023 sk_canvas_t* canvas = sk_surface_get_canvas(surface);
24 sk_paint_t* paint = sk_paint_new();
25
26 sk_shader_tilemode_t tilemode = CLAMP_SK_SHADER_TILEMODE;
27 sk_point_t point = {0.0f, 0.0f};
28 sk_point_t point2 = {30.0f, 40.0f};
29 sk_color_t colors[] = {
30 (sk_color_t)sk_color_set_argb(0xFF, 0x00, 0x00, 0xFF),
31 (sk_color_t)sk_color_set_argb(0xFF, 0x00, 0xFF, 0x00)
32 };
33 sk_shader_t* shader;
34
35 shader = sk_shader_new_radial_gradient(
halcanary96fcdcc2015-08-27 07:41:13 -070036 &point, 1.0f, colors, nullptr, 2, tilemode, nullptr);
37 REPORTER_ASSERT(reporter, shader != nullptr);
halcanary65cc3e42015-08-12 07:37:34 -070038 sk_paint_set_shader(paint, shader);
39 sk_shader_unref(shader);
40 sk_canvas_draw_paint(canvas, paint);
41
halcanary96fcdcc2015-08-27 07:41:13 -070042 shader = sk_shader_new_sweep_gradient(&point, colors, nullptr, 2, nullptr);
43 REPORTER_ASSERT(reporter, shader != nullptr);
halcanary65cc3e42015-08-12 07:37:34 -070044 sk_paint_set_shader(paint, shader);
45 sk_shader_unref(shader);
46 sk_canvas_draw_paint(canvas, paint);
47
48 shader = sk_shader_new_two_point_conical_gradient(
halcanary96fcdcc2015-08-27 07:41:13 -070049 &point, 10.0f, &point2, 50.0f, colors, nullptr, 2, tilemode, nullptr);
50 REPORTER_ASSERT(reporter, shader != nullptr);
halcanary65cc3e42015-08-12 07:37:34 -070051 sk_paint_set_shader(paint, shader);
52 sk_shader_unref(shader);
53 sk_canvas_draw_paint(canvas, paint);
54
55 sk_paint_delete(paint);
56 sk_surface_unref(surface);
Mike Reed66c505f2018-09-28 11:27:57 -040057 sk_imageinfo_delete(info);
halcanary65cc3e42015-08-12 07:37:34 -070058}
59
reede3323962014-10-24 11:16:19 -070060static void test_c(skiatest::Reporter* reporter) {
Mike Reed66c505f2018-09-28 11:27:57 -040061 sk_imageinfo_t* info = sk_imageinfo_new(1, 1, RGBA_8888_SK_COLORTYPE, PREMUL_SK_ALPHATYPE,
62 NULL);
reede3323962014-10-24 11:16:19 -070063 uint32_t pixel[1] = { 0 };
robertphillips702edbd2015-06-23 06:26:08 -070064 sk_surfaceprops_t surfaceProps = { UNKNOWN_SK_PIXELGEOMETRY };
reede3323962014-10-24 11:16:19 -070065
Mike Reed66c505f2018-09-28 11:27:57 -040066 sk_surface_t* surface = sk_surface_new_raster_direct(info, pixel, sizeof(uint32_t),
robertphillips702edbd2015-06-23 06:26:08 -070067 &surfaceProps);
reede3323962014-10-24 11:16:19 -070068 sk_paint_t* paint = sk_paint_new();
69
70 sk_canvas_t* canvas = sk_surface_get_canvas(surface);
71 sk_canvas_draw_paint(canvas, paint);
72 REPORTER_ASSERT(reporter, 0xFF000000 == pixel[0]);
73
74 sk_paint_set_color(paint, sk_color_set_argb(0xFF, 0xFF, 0xFF, 0xFF));
75 sk_canvas_draw_paint(canvas, paint);
76 REPORTER_ASSERT(reporter, 0xFFFFFFFF == pixel[0]);
77
halcanary7568d0b2015-07-31 13:38:06 -070078 sk_paint_set_xfermode_mode(paint, SRC_SK_XFERMODE_MODE);
79 sk_paint_set_color(paint, sk_color_set_argb(0x80, 0x80, 0x80, 0x80));
80 sk_canvas_draw_paint(canvas, paint);
81 REPORTER_ASSERT(reporter, 0x80404040 == pixel[0]);
82
reede3323962014-10-24 11:16:19 -070083 sk_paint_delete(paint);
reedafa278e2014-11-24 19:11:48 -080084 sk_surface_unref(surface);
Mike Reed66c505f2018-09-28 11:27:57 -040085 sk_imageinfo_delete(info);
reede3323962014-10-24 11:16:19 -070086}
87
88DEF_TEST(C_API, reporter) {
89 test_c(reporter);
halcanary65cc3e42015-08-12 07:37:34 -070090 shader_test(reporter);
reede3323962014-10-24 11:16:19 -070091}