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 | |
halcanary | 219f18f | 2015-09-01 10:01:38 -0700 | [diff] [blame] | 8 | #include "Test.h" |
reed | 73c2501 | 2014-11-17 06:15:42 -0800 | [diff] [blame] | 9 | #include "sk_canvas.h" |
| 10 | #include "sk_paint.h" |
halcanary | 65cc3e4 | 2015-08-12 07:37:34 -0700 | [diff] [blame] | 11 | #include "sk_shader.h" |
Ben Wagner | eed6128 | 2018-04-17 14:14:51 -0400 | [diff] [blame] | 12 | #include "sk_surface.h" |
| 13 | #include "sk_types.h" |
| 14 | |
| 15 | #include <stdint.h> |
reed | 73c2501 | 2014-11-17 06:15:42 -0800 | [diff] [blame] | 16 | |
halcanary | 65cc3e4 | 2015-08-12 07:37:34 -0700 | [diff] [blame] | 17 | static void shader_test(skiatest::Reporter* reporter) { |
| 18 | sk_imageinfo_t info = |
| 19 | {64, 64, sk_colortype_get_default_8888(), PREMUL_SK_ALPHATYPE}; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 20 | sk_surface_t* surface = sk_surface_new_raster(&info, nullptr); |
halcanary | 65cc3e4 | 2015-08-12 07:37:34 -0700 | [diff] [blame] | 21 | 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( |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 34 | &point, 1.0f, colors, nullptr, 2, tilemode, nullptr); |
| 35 | REPORTER_ASSERT(reporter, shader != nullptr); |
halcanary | 65cc3e4 | 2015-08-12 07:37:34 -0700 | [diff] [blame] | 36 | sk_paint_set_shader(paint, shader); |
| 37 | sk_shader_unref(shader); |
| 38 | sk_canvas_draw_paint(canvas, paint); |
| 39 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 40 | shader = sk_shader_new_sweep_gradient(&point, colors, nullptr, 2, nullptr); |
| 41 | REPORTER_ASSERT(reporter, shader != nullptr); |
halcanary | 65cc3e4 | 2015-08-12 07:37:34 -0700 | [diff] [blame] | 42 | 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( |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 47 | &point, 10.0f, &point2, 50.0f, colors, nullptr, 2, tilemode, nullptr); |
| 48 | REPORTER_ASSERT(reporter, shader != nullptr); |
halcanary | 65cc3e4 | 2015-08-12 07:37:34 -0700 | [diff] [blame] | 49 | 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 | |
reed | e332396 | 2014-10-24 11:16:19 -0700 | [diff] [blame] | 57 | static 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 }; |
robertphillips | 702edbd | 2015-06-23 06:26:08 -0700 | [diff] [blame] | 64 | sk_surfaceprops_t surfaceProps = { UNKNOWN_SK_PIXELGEOMETRY }; |
reed | e332396 | 2014-10-24 11:16:19 -0700 | [diff] [blame] | 65 | |
robertphillips | 702edbd | 2015-06-23 06:26:08 -0700 | [diff] [blame] | 66 | sk_surface_t* surface = sk_surface_new_raster_direct(&info, pixel, sizeof(uint32_t), |
| 67 | &surfaceProps); |
reed | e332396 | 2014-10-24 11:16:19 -0700 | [diff] [blame] | 68 | 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 | |
halcanary | 7568d0b | 2015-07-31 13:38:06 -0700 | [diff] [blame] | 78 | 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 | |
reed | e332396 | 2014-10-24 11:16:19 -0700 | [diff] [blame] | 83 | sk_paint_delete(paint); |
reed | afa278e | 2014-11-24 19:11:48 -0800 | [diff] [blame] | 84 | sk_surface_unref(surface); |
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 | } |