blob: 425b264718edcef67161f967600a725029898f9d [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
halcanary219f18f2015-09-01 10:01:38 -07008#include "Test.h"
reed73c25012014-11-17 06:15:42 -08009#include "sk_canvas.h"
10#include "sk_paint.h"
halcanary65cc3e42015-08-12 07:37:34 -070011#include "sk_shader.h"
Ben Wagnereed61282018-04-17 14:14:51 -040012#include "sk_surface.h"
13#include "sk_types.h"
14
15#include <stdint.h>
reed73c25012014-11-17 06:15:42 -080016
halcanary65cc3e42015-08-12 07:37:34 -070017static void shader_test(skiatest::Reporter* reporter) {
18 sk_imageinfo_t info =
19 {64, 64, sk_colortype_get_default_8888(), PREMUL_SK_ALPHATYPE};
halcanary96fcdcc2015-08-27 07:41:13 -070020 sk_surface_t* surface = sk_surface_new_raster(&info, nullptr);
halcanary65cc3e42015-08-12 07:37:34 -070021 sk_canvas_t* canvas = sk_surface_get_canvas(surface);
22 sk_paint_t* paint = sk_paint_new();
23
24 sk_shader_tilemode_t tilemode = CLAMP_SK_SHADER_TILEMODE;
25 sk_point_t point = {0.0f, 0.0f};
26 sk_point_t point2 = {30.0f, 40.0f};
27 sk_color_t colors[] = {
28 (sk_color_t)sk_color_set_argb(0xFF, 0x00, 0x00, 0xFF),
29 (sk_color_t)sk_color_set_argb(0xFF, 0x00, 0xFF, 0x00)
30 };
31 sk_shader_t* shader;
32
33 shader = sk_shader_new_radial_gradient(
halcanary96fcdcc2015-08-27 07:41:13 -070034 &point, 1.0f, colors, nullptr, 2, tilemode, nullptr);
35 REPORTER_ASSERT(reporter, shader != nullptr);
halcanary65cc3e42015-08-12 07:37:34 -070036 sk_paint_set_shader(paint, shader);
37 sk_shader_unref(shader);
38 sk_canvas_draw_paint(canvas, paint);
39
halcanary96fcdcc2015-08-27 07:41:13 -070040 shader = sk_shader_new_sweep_gradient(&point, colors, nullptr, 2, nullptr);
41 REPORTER_ASSERT(reporter, shader != nullptr);
halcanary65cc3e42015-08-12 07:37:34 -070042 sk_paint_set_shader(paint, shader);
43 sk_shader_unref(shader);
44 sk_canvas_draw_paint(canvas, paint);
45
46 shader = sk_shader_new_two_point_conical_gradient(
halcanary96fcdcc2015-08-27 07:41:13 -070047 &point, 10.0f, &point2, 50.0f, colors, nullptr, 2, tilemode, nullptr);
48 REPORTER_ASSERT(reporter, shader != nullptr);
halcanary65cc3e42015-08-12 07:37:34 -070049 sk_paint_set_shader(paint, shader);
50 sk_shader_unref(shader);
51 sk_canvas_draw_paint(canvas, paint);
52
53 sk_paint_delete(paint);
54 sk_surface_unref(surface);
55}
56
reede3323962014-10-24 11:16:19 -070057static void test_c(skiatest::Reporter* reporter) {
58 sk_colortype_t ct = sk_colortype_get_default_8888();
59
60 sk_imageinfo_t info = {
61 1, 1, ct, PREMUL_SK_ALPHATYPE
62 };
63 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
robertphillips702edbd2015-06-23 06:26:08 -070066 sk_surface_t* surface = sk_surface_new_raster_direct(&info, pixel, sizeof(uint32_t),
67 &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);
reede3323962014-10-24 11:16:19 -070085}
86
87DEF_TEST(C_API, reporter) {
88 test_c(reporter);
halcanary65cc3e42015-08-12 07:37:34 -070089 shader_test(reporter);
reede3323962014-10-24 11:16:19 -070090}