reed | e332396 | 2014-10-24 11:16:19 -0700 | [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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame^] | 8 | #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 Wagner | eed6128 | 2018-04-17 14:14:51 -0400 | [diff] [blame] | 15 | |
| 16 | #include <stdint.h> |
reed | 73c2501 | 2014-11-17 06:15:42 -0800 | [diff] [blame] | 17 | |
halcanary | 65cc3e4 | 2015-08-12 07:37:34 -0700 | [diff] [blame] | 18 | static void shader_test(skiatest::Reporter* reporter) { |
Mike Reed | 66c505f | 2018-09-28 11:27:57 -0400 | [diff] [blame] | 19 | sk_imageinfo_t* info = sk_imageinfo_new(64, 64, RGBA_8888_SK_COLORTYPE, PREMUL_SK_ALPHATYPE, |
| 20 | NULL); |
| 21 | sk_surface_t* surface = sk_surface_new_raster(info, nullptr); |
halcanary | 65cc3e4 | 2015-08-12 07:37:34 -0700 | [diff] [blame] | 22 | sk_canvas_t* canvas = sk_surface_get_canvas(surface); |
| 23 | sk_paint_t* paint = sk_paint_new(); |
| 24 | |
| 25 | sk_shader_tilemode_t tilemode = CLAMP_SK_SHADER_TILEMODE; |
| 26 | sk_point_t point = {0.0f, 0.0f}; |
| 27 | sk_point_t point2 = {30.0f, 40.0f}; |
| 28 | sk_color_t colors[] = { |
| 29 | (sk_color_t)sk_color_set_argb(0xFF, 0x00, 0x00, 0xFF), |
| 30 | (sk_color_t)sk_color_set_argb(0xFF, 0x00, 0xFF, 0x00) |
| 31 | }; |
| 32 | sk_shader_t* shader; |
| 33 | |
| 34 | shader = sk_shader_new_radial_gradient( |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 35 | &point, 1.0f, colors, nullptr, 2, tilemode, nullptr); |
| 36 | REPORTER_ASSERT(reporter, shader != nullptr); |
halcanary | 65cc3e4 | 2015-08-12 07:37:34 -0700 | [diff] [blame] | 37 | sk_paint_set_shader(paint, shader); |
| 38 | sk_shader_unref(shader); |
| 39 | sk_canvas_draw_paint(canvas, paint); |
| 40 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 41 | shader = sk_shader_new_sweep_gradient(&point, colors, nullptr, 2, nullptr); |
| 42 | REPORTER_ASSERT(reporter, shader != nullptr); |
halcanary | 65cc3e4 | 2015-08-12 07:37:34 -0700 | [diff] [blame] | 43 | sk_paint_set_shader(paint, shader); |
| 44 | sk_shader_unref(shader); |
| 45 | sk_canvas_draw_paint(canvas, paint); |
| 46 | |
| 47 | shader = sk_shader_new_two_point_conical_gradient( |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 48 | &point, 10.0f, &point2, 50.0f, colors, nullptr, 2, tilemode, nullptr); |
| 49 | REPORTER_ASSERT(reporter, shader != nullptr); |
halcanary | 65cc3e4 | 2015-08-12 07:37:34 -0700 | [diff] [blame] | 50 | sk_paint_set_shader(paint, shader); |
| 51 | sk_shader_unref(shader); |
| 52 | sk_canvas_draw_paint(canvas, paint); |
| 53 | |
| 54 | sk_paint_delete(paint); |
| 55 | sk_surface_unref(surface); |
Mike Reed | 66c505f | 2018-09-28 11:27:57 -0400 | [diff] [blame] | 56 | sk_imageinfo_delete(info); |
halcanary | 65cc3e4 | 2015-08-12 07:37:34 -0700 | [diff] [blame] | 57 | } |
| 58 | |
reed | e332396 | 2014-10-24 11:16:19 -0700 | [diff] [blame] | 59 | static void test_c(skiatest::Reporter* reporter) { |
Mike Reed | 66c505f | 2018-09-28 11:27:57 -0400 | [diff] [blame] | 60 | sk_imageinfo_t* info = sk_imageinfo_new(1, 1, RGBA_8888_SK_COLORTYPE, PREMUL_SK_ALPHATYPE, |
| 61 | NULL); |
reed | e332396 | 2014-10-24 11:16:19 -0700 | [diff] [blame] | 62 | uint32_t pixel[1] = { 0 }; |
robertphillips | 702edbd | 2015-06-23 06:26:08 -0700 | [diff] [blame] | 63 | sk_surfaceprops_t surfaceProps = { UNKNOWN_SK_PIXELGEOMETRY }; |
reed | e332396 | 2014-10-24 11:16:19 -0700 | [diff] [blame] | 64 | |
Mike Reed | 66c505f | 2018-09-28 11:27:57 -0400 | [diff] [blame] | 65 | sk_surface_t* surface = sk_surface_new_raster_direct(info, pixel, sizeof(uint32_t), |
robertphillips | 702edbd | 2015-06-23 06:26:08 -0700 | [diff] [blame] | 66 | &surfaceProps); |
reed | e332396 | 2014-10-24 11:16:19 -0700 | [diff] [blame] | 67 | sk_paint_t* paint = sk_paint_new(); |
| 68 | |
| 69 | sk_canvas_t* canvas = sk_surface_get_canvas(surface); |
| 70 | sk_canvas_draw_paint(canvas, paint); |
| 71 | REPORTER_ASSERT(reporter, 0xFF000000 == pixel[0]); |
| 72 | |
| 73 | sk_paint_set_color(paint, sk_color_set_argb(0xFF, 0xFF, 0xFF, 0xFF)); |
| 74 | sk_canvas_draw_paint(canvas, paint); |
| 75 | REPORTER_ASSERT(reporter, 0xFFFFFFFF == pixel[0]); |
| 76 | |
halcanary | 7568d0b | 2015-07-31 13:38:06 -0700 | [diff] [blame] | 77 | sk_paint_set_xfermode_mode(paint, SRC_SK_XFERMODE_MODE); |
| 78 | sk_paint_set_color(paint, sk_color_set_argb(0x80, 0x80, 0x80, 0x80)); |
| 79 | sk_canvas_draw_paint(canvas, paint); |
| 80 | REPORTER_ASSERT(reporter, 0x80404040 == pixel[0]); |
| 81 | |
reed | e332396 | 2014-10-24 11:16:19 -0700 | [diff] [blame] | 82 | sk_paint_delete(paint); |
reed | afa278e | 2014-11-24 19:11:48 -0800 | [diff] [blame] | 83 | sk_surface_unref(surface); |
Mike Reed | 66c505f | 2018-09-28 11:27:57 -0400 | [diff] [blame] | 84 | sk_imageinfo_delete(info); |
reed | e332396 | 2014-10-24 11:16:19 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | DEF_TEST(C_API, reporter) { |
| 88 | test_c(reporter); |
halcanary | 65cc3e4 | 2015-08-12 07:37:34 -0700 | [diff] [blame] | 89 | shader_test(reporter); |
reed | e332396 | 2014-10-24 11:16:19 -0700 | [diff] [blame] | 90 | } |