1 #if LV_BUILD_TEST
2 #include "../lvgl.h"
3 #include "../../lvgl_private.h"
4 #include "lv_test_helpers.h"
5
6 #include "unity/unity.h"
7
setUp(void)8 void setUp(void)
9 {
10 /* Function run before every test */
11 }
12
tearDown(void)13 void tearDown(void)
14 {
15 /* Function run after every test */
16 }
17
draw_shapes(lv_layer_t * layer)18 static void draw_shapes(lv_layer_t * layer)
19 {
20 lv_vector_dsc_t * ctx = lv_vector_dsc_create(layer);
21
22 lv_area_t rect = {0, 0, 640, 480};
23 lv_vector_dsc_set_fill_color(ctx, lv_color_white());
24 lv_vector_clear_area(ctx, &rect);
25
26 lv_vector_path_t * path = lv_vector_path_create(LV_VECTOR_PATH_QUALITY_MEDIUM);
27
28 lv_area_t rect1 = {50, 50, 150, 150};
29 lv_vector_dsc_identity(ctx);
30 lv_vector_path_clear(path);
31 lv_vector_path_append_rect(path, &rect1, 0, 0);
32 lv_vector_dsc_set_fill_color(ctx, lv_color_make(0xff, 0x00, 0x00));
33 lv_vector_dsc_add_path(ctx, path);
34
35 lv_vector_path_clear(path);
36 lv_vector_path_append_rect(path, &rect1, 20, 20);
37 lv_vector_dsc_translate(ctx, 150, 0);
38 lv_vector_dsc_set_fill_color(ctx, lv_color_make(0x00, 0x00, 0xff));
39 lv_vector_dsc_add_path(ctx, path);
40
41 lv_fpoint_t pc = {100, 100};
42 lv_vector_path_clear(path);
43 lv_vector_path_append_circle(path, &pc, 50, 50);
44 lv_vector_dsc_translate(ctx, 150, 0);
45 lv_vector_dsc_set_fill_color32(ctx, lv_color_to_32(lv_color_make(0x00, 0xff, 0x00), 0x80));
46 lv_vector_dsc_add_path(ctx, path);
47
48 lv_vector_dsc_identity(ctx);
49 lv_vector_dsc_translate(ctx, 0, 150);
50
51 lv_gradient_stop_t stops[2];
52 lv_memzero(stops, sizeof(stops));
53 stops[0].color = lv_color_hex(0xffffff);
54 stops[0].opa = LV_OPA_COVER;
55 stops[0].frac = 0;
56 stops[1].color = lv_color_hex(0x000000);
57 stops[1].opa = LV_OPA_COVER;
58 stops[1].frac = 255;
59
60 lv_matrix_t mt;
61 lv_matrix_identity(&mt);
62 lv_vector_dsc_set_fill_transform(ctx, &mt);
63 lv_vector_dsc_set_fill_radial_gradient(ctx, 100, 100, 50);
64 lv_vector_dsc_set_fill_gradient_color_stops(ctx, stops, 2);
65 lv_vector_dsc_set_fill_gradient_spread(ctx, LV_VECTOR_GRADIENT_SPREAD_PAD);
66 lv_vector_dsc_add_path(ctx, path);
67
68 lv_vector_dsc_identity(ctx);
69 lv_vector_dsc_translate(ctx, 150, 150);
70 lv_draw_image_dsc_t img_dsc;
71 lv_draw_image_dsc_init(&img_dsc);
72 LV_IMAGE_DECLARE(test_image_cogwheel_argb8888);
73 img_dsc.header = test_image_cogwheel_argb8888.header;
74 img_dsc.src = &test_image_cogwheel_argb8888;
75 lv_vector_dsc_set_fill_image(ctx, &img_dsc);
76 lv_matrix_identity(&mt);
77 lv_matrix_translate(&mt, 50, 50);
78 //lv_matrix_rotate(&mt, 45);
79 //lv_matrix_scale(&mt, 1.5f, 1.5f);
80 lv_matrix_translate(&mt, -50, -50);
81 lv_vector_dsc_set_fill_transform(ctx, &mt);
82 lv_vector_dsc_add_path(ctx, path);
83
84 lv_vector_dsc_identity(ctx);
85 lv_vector_dsc_translate(ctx, 300, 150);
86 lv_vector_dsc_set_fill_color(ctx, lv_color_make(0xff, 0x00, 0x00));
87 lv_vector_dsc_add_path(ctx, path);
88
89 lv_vector_path_clear(path);
90 lv_vector_path_append_rect(path, &rect1, 20, 20);
91 lv_area_t rect2 = {80, 80, 120, 120};
92 lv_vector_path_append_rect(path, &rect2, 15, 15);
93 lv_vector_dsc_translate(ctx, 50, 0);
94 lv_vector_dsc_set_fill_color(ctx, lv_color_make(0x00, 0x80, 0xff));
95 lv_vector_dsc_set_blend_mode(ctx, LV_VECTOR_BLEND_MULTIPLY);
96 lv_vector_dsc_set_fill_rule(ctx, LV_VECTOR_FILL_EVENODD);
97 lv_vector_dsc_add_path(ctx, path);
98
99 lv_vector_path_clear(path);
100 lv_vector_dsc_identity(ctx);
101 lv_vector_dsc_set_blend_mode(ctx, LV_VECTOR_BLEND_SRC_OVER);
102
103 rect = (lv_area_t) {
104 500, 50, 550, 100
105 };
106 lv_vector_dsc_set_fill_color(ctx, lv_color_lighten(lv_color_black(), 50));
107 lv_vector_clear_area(ctx, &rect); // clear screen
108
109 lv_fpoint_t p = {500, 100}; /* Center */
110 lv_vector_dsc_set_stroke_color(ctx, lv_color_make(0x00, 0xff, 0xff));
111 lv_vector_dsc_set_stroke_opa(ctx, LV_OPA_COVER);
112 lv_vector_dsc_set_stroke_width(ctx, 2.0f);
113 lv_vector_dsc_set_stroke_dash(ctx, NULL, 0);
114 lv_vector_dsc_set_fill_opa(ctx, LV_OPA_TRANSP);
115 lv_vector_path_move_to(path, &p);
116 lv_fpoint_t temp = {p.x + 50, p.y};
117 lv_vector_path_line_to(path, &temp);
118 lv_vector_path_append_arc(path, &p, 50, 0, -90, false);
119 lv_vector_path_line_to(path, &p);
120 lv_vector_path_close(path);
121
122 lv_vector_dsc_add_path(ctx, path);
123
124 /* Below code has same effect as above one but with solid fill */
125 lv_vector_dsc_set_fill_color(ctx, lv_color_make(0xFF, 0x00, 0x00));
126 lv_vector_dsc_set_fill_opa(ctx, LV_OPA_COVER);
127 lv_vector_path_clear(path);
128 lv_vector_path_append_arc(path, &p, 50, 45, 45, true);
129 lv_vector_dsc_add_path(ctx, path); // draw a path
130
131 lv_draw_vector(ctx);
132 lv_vector_path_delete(path);
133 lv_vector_dsc_delete(ctx);
134 }
135
draw_lines(lv_layer_t * layer)136 static void draw_lines(lv_layer_t * layer)
137 {
138 lv_vector_dsc_t * ctx = lv_vector_dsc_create(layer);
139
140 lv_area_t rect = {0, 0, 640, 480};
141 lv_vector_dsc_set_fill_color(ctx, lv_color_white());
142 lv_vector_clear_area(ctx, &rect);
143
144 lv_vector_path_t * path = lv_vector_path_create(LV_VECTOR_PATH_QUALITY_MEDIUM);
145
146 lv_vector_dsc_set_stroke_opa(ctx, LV_OPA_COVER);
147 lv_vector_dsc_set_fill_opa(ctx, LV_OPA_0);
148
149 lv_fpoint_t pts1[] = {{50, 50}, {400, 50}};
150 lv_vector_path_clear(path);
151 lv_vector_path_move_to(path, &pts1[0]);
152 lv_vector_path_line_to(path, &pts1[1]);
153 lv_vector_dsc_add_path(ctx, path);
154
155 lv_fpoint_t pts2[] = {{50, 80}, {400, 80}};
156 lv_vector_path_clear(path);
157 lv_vector_path_move_to(path, &pts2[0]);
158 lv_vector_path_line_to(path, &pts2[1]);
159 lv_vector_dsc_set_stroke_color(ctx, lv_color_make(0xff, 0x00, 0x00));
160 lv_vector_dsc_set_stroke_width(ctx, 3.0f);
161 lv_vector_dsc_add_path(ctx, path);
162
163 lv_fpoint_t pts3[] = {{50, 120}, {400, 120}};
164 lv_vector_path_clear(path);
165 lv_vector_path_move_to(path, &pts3[0]);
166 lv_vector_path_line_to(path, &pts3[1]);
167 lv_vector_dsc_set_stroke_color(ctx, lv_color_make(0x00, 0xff, 0x00));
168 lv_vector_dsc_set_stroke_width(ctx, 5.0f);
169 float dashes[] = {10, 15, 20, 12};
170 lv_vector_dsc_set_stroke_dash(ctx, dashes, 4);
171 lv_vector_dsc_add_path(ctx, path);
172
173 lv_fpoint_t pts4[] = {{100, 150}, {50, 250}, {150, 250}};
174 lv_vector_path_clear(path);
175 lv_vector_path_move_to(path, &pts4[0]);
176 lv_vector_path_line_to(path, &pts4[1]);
177 lv_vector_path_line_to(path, &pts4[2]);
178 lv_vector_dsc_set_stroke_color(ctx, lv_color_make(0x00, 0x00, 0x00));
179 lv_vector_dsc_set_stroke_width(ctx, 10.0f);
180 lv_vector_dsc_set_stroke_dash(ctx, NULL, 0);
181 lv_vector_dsc_set_stroke_join(ctx, LV_VECTOR_STROKE_JOIN_MITER);
182 lv_vector_dsc_add_path(ctx, path);
183
184 lv_vector_dsc_translate(ctx, 150, 0);
185 lv_vector_dsc_set_stroke_join(ctx, LV_VECTOR_STROKE_JOIN_BEVEL);
186 lv_vector_dsc_add_path(ctx, path);
187
188 lv_vector_dsc_translate(ctx, 150, 0);
189 lv_vector_dsc_set_stroke_join(ctx, LV_VECTOR_STROKE_JOIN_ROUND);
190 lv_vector_dsc_add_path(ctx, path);
191
192 lv_fpoint_t pts5[] = {{50, 300}, {150, 300}};
193 lv_vector_dsc_identity(ctx);
194 lv_vector_path_clear(path);
195 lv_vector_path_move_to(path, &pts5[0]);
196 lv_vector_path_line_to(path, &pts5[1]);
197 lv_vector_dsc_set_stroke_cap(ctx, LV_VECTOR_STROKE_CAP_BUTT);
198 lv_vector_dsc_add_path(ctx, path);
199
200 lv_vector_dsc_translate(ctx, 0, 40);
201 lv_vector_dsc_set_stroke_cap(ctx, LV_VECTOR_STROKE_CAP_SQUARE);
202 lv_vector_dsc_add_path(ctx, path);
203
204 lv_vector_dsc_translate(ctx, 0, 40);
205 lv_vector_dsc_set_stroke_cap(ctx, LV_VECTOR_STROKE_CAP_ROUND);
206 lv_vector_dsc_add_path(ctx, path);
207
208 lv_area_t rect1 = {250, 300, 350, 400};
209 lv_vector_dsc_identity(ctx);
210 lv_vector_path_clear(path);
211 lv_vector_path_append_rect(path, &rect1, 0, 0);
212
213 lv_gradient_stop_t stops[2];
214 lv_memzero(stops, sizeof(stops));
215 stops[0].color = lv_color_hex(0xff0000);
216 stops[0].opa = LV_OPA_COVER;
217 stops[0].frac = 0;
218 stops[1].color = lv_color_hex(0x00ff00);
219 stops[1].opa = LV_OPA_COVER;
220 stops[1].frac = 255;
221
222 lv_matrix_t mt;
223 lv_matrix_identity(&mt);
224 lv_matrix_rotate(&mt, 5);
225 lv_matrix_translate(&mt, 20, 20);
226 lv_vector_dsc_set_stroke_transform(ctx, &mt);
227 lv_vector_dsc_set_stroke_join(ctx, LV_VECTOR_STROKE_JOIN_MITER);
228 lv_vector_dsc_set_stroke_linear_gradient(ctx, 250, 300, 350, 300);
229 lv_vector_dsc_set_stroke_gradient_color_stops(ctx, stops, 2);
230 lv_vector_dsc_set_stroke_gradient_spread(ctx, LV_VECTOR_GRADIENT_SPREAD_REFLECT);
231 lv_vector_dsc_add_path(ctx, path); // draw a path
232
233 lv_draw_vector(ctx);
234 lv_vector_path_delete(path);
235 lv_vector_dsc_delete(ctx);
236 }
237
canvas_draw(const char * name,void (* draw_cb)(lv_layer_t *))238 static void canvas_draw(const char * name, void (*draw_cb)(lv_layer_t *))
239 {
240 LV_UNUSED(name);
241 lv_obj_t * canvas = lv_canvas_create(lv_screen_active());
242 lv_draw_buf_t * draw_buf = lv_draw_buf_create(640, 480, LV_COLOR_FORMAT_ARGB8888, LV_STRIDE_AUTO);
243 TEST_ASSERT_NOT_NULL(draw_buf);
244
245 lv_draw_buf_clear(draw_buf, NULL);
246 lv_canvas_set_draw_buf(canvas, draw_buf);
247 lv_canvas_fill_bg(canvas, lv_color_make(0xff, 0xff, 0xff), 255);
248
249 lv_layer_t layer;
250 lv_canvas_init_layer(canvas, &layer);
251
252 draw_cb(&layer);
253
254 lv_canvas_finish_layer(canvas, &layer);
255
256 #ifndef NON_AMD64_BUILD
257 char fn_buf[64];
258 lv_snprintf(fn_buf, sizeof(fn_buf), "draw/vector_%s.lp64.png", name);
259 TEST_ASSERT_EQUAL_SCREENSHOT(fn_buf);
260 #else
261 char fn_buf[64];
262 lv_snprintf(fn_buf, sizeof(fn_buf), "draw/vector_%s.lp32.png", name);
263 TEST_ASSERT_EQUAL_SCREENSHOT(fn_buf);
264 #endif
265
266 lv_image_cache_drop(draw_buf);
267 lv_draw_buf_destroy(draw_buf);
268 lv_obj_delete(canvas);
269 }
270
test_transform(void)271 void test_transform(void)
272 {
273 lv_matrix_t matrix;
274 lv_matrix_identity(&matrix);
275 lv_matrix_translate(&matrix, 100, 100);
276
277 lv_fpoint_t p = {10, 10};
278 lv_matrix_transform_point(&matrix, &p);
279
280 TEST_ASSERT_EQUAL_FLOAT(110.0f, p.x);
281 TEST_ASSERT_EQUAL_FLOAT(110.0f, p.y);
282
283 lv_vector_path_t * path = lv_vector_path_create(LV_VECTOR_PATH_QUALITY_MEDIUM);
284 lv_vector_path_move_to(path, &p);
285
286 lv_fpoint_t p2 = {20, 20};
287 lv_vector_path_line_to(path, &p2);
288 lv_matrix_transform_path(&matrix, path);
289
290 lv_fpoint_t * pt = lv_array_at(&path->points, 0);
291
292 TEST_ASSERT_EQUAL_FLOAT(210.0f, pt[0].x);
293 TEST_ASSERT_EQUAL_FLOAT(210.0f, pt[0].y);
294 TEST_ASSERT_EQUAL_FLOAT(120.0f, pt[1].x);
295 TEST_ASSERT_EQUAL_FLOAT(120.0f, pt[1].y);
296
297 lv_vector_path_delete(path);
298 }
299
test_draw_lines(void)300 void test_draw_lines(void)
301 {
302 canvas_draw("draw_lines", draw_lines);
303 }
304
test_draw_shapes(void)305 void test_draw_shapes(void)
306 {
307 canvas_draw("draw_shapes", draw_shapes);
308 }
309
event_cb(lv_event_t * e)310 static void event_cb(lv_event_t * e)
311 {
312 lv_layer_t * layer = lv_event_get_layer(e);
313 lv_obj_t * obj = lv_event_get_current_target_obj(e);
314
315 lv_vector_dsc_t * dsc = lv_vector_dsc_create(layer);
316 lv_vector_path_t * path = lv_vector_path_create(LV_VECTOR_PATH_QUALITY_MEDIUM);
317
318 lv_fpoint_t pts[] = {{10, 10}, {130, 130}, {10, 130}};
319 lv_vector_path_move_to(path, &pts[0]);
320 lv_vector_path_line_to(path, &pts[1]);
321 lv_vector_path_line_to(path, &pts[2]);
322 lv_vector_path_close(path);
323
324 lv_vector_dsc_translate(dsc, obj->coords.x1, obj->coords.y1);
325 lv_vector_dsc_set_fill_color(dsc, lv_color_make(0x00, 0x80, 0xff));
326 lv_vector_dsc_add_path(dsc, path);
327
328 lv_draw_vector(dsc);
329 lv_vector_path_delete(path);
330 lv_vector_dsc_delete(dsc);
331 }
332
test_draw_during_rendering(void)333 void test_draw_during_rendering(void)
334 {
335 lv_obj_t * obj = lv_obj_create(lv_screen_active());
336 lv_obj_center(obj);
337 lv_obj_add_event_cb(obj, event_cb, LV_EVENT_DRAW_MAIN, NULL);
338
339 TEST_ASSERT_EQUAL_SCREENSHOT("draw/vector_draw_during_rendering.png");
340 }
341
342 #endif
343