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