1 /*
2  * Copyright (c) 2024 TOKITA Hiroshi
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/drivers/display.h>
8 #include <zephyr/ztest.h>
9 #include <zephyr/device.h>
10 #include <zephyr/logging/log.h>
11 #include <zephyr/display/cfb.h>
12 
13 #include "testdata.h"
14 #include "utils.h"
15 
16 LOG_MODULE_REGISTER(draw_line, CONFIG_DISPLAY_LOG_LEVEL);
17 
18 static const struct device *dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_display));
19 static const uint32_t display_width = DT_PROP(DT_CHOSEN(zephyr_display), width);
20 static const uint32_t display_height = DT_PROP(DT_CHOSEN(zephyr_display), height);
21 
22 /**
23  * Fill the buffer with 0 before running tests.
24  */
cfb_test_before(void * text_fixture)25 static void cfb_test_before(void *text_fixture)
26 {
27 	struct display_buffer_descriptor desc = {
28 		.height = display_height,
29 		.pitch = display_width,
30 		.width = display_width,
31 		.buf_size = display_height * display_width / 8,
32 	};
33 
34 	memset(read_buffer, 0, sizeof(read_buffer));
35 	zassert_ok(display_write(dev, 0, 0, &desc, read_buffer));
36 
37 	zassert_ok(display_blanking_off(dev));
38 
39 	zassert_ok(cfb_framebuffer_init(dev));
40 }
41 
cfb_test_after(void * test_fixture)42 static void cfb_test_after(void *test_fixture)
43 {
44 	cfb_framebuffer_deinit(dev);
45 }
46 
ZTEST(draw_line,test_draw_line_top_end)47 ZTEST(draw_line, test_draw_line_top_end)
48 {
49 	struct cfb_position start = {0, 0};
50 	struct cfb_position end = {display_width, 0};
51 
52 	zassert_ok(cfb_draw_line(dev, &start, &end));
53 	zassert_ok(cfb_framebuffer_finalize(dev));
54 
55 	zassert_true(verify_color_inside_rect(0, 0, display_width, 1, 0xFFFFFF));
56 }
57 
ZTEST(draw_line,test_draw_line_left_end)58 ZTEST(draw_line, test_draw_line_left_end)
59 {
60 	struct cfb_position start = {0, 0};
61 	struct cfb_position end = {0, display_height};
62 
63 	zassert_ok(cfb_draw_line(dev, &start, &end));
64 	zassert_ok(cfb_framebuffer_finalize(dev));
65 
66 	zassert_true(verify_color_inside_rect(0, 0, 1, display_height, 0xFFFFFF));
67 }
68 
ZTEST(draw_line,test_draw_right_end)69 ZTEST(draw_line, test_draw_right_end)
70 {
71 	struct cfb_position start = {display_width - 1, 0};
72 	struct cfb_position end = {display_width - 1, display_height};
73 
74 	zassert_ok(cfb_draw_line(dev, &start, &end));
75 	zassert_ok(cfb_framebuffer_finalize(dev));
76 
77 	zassert_true(verify_color_inside_rect(display_width - 1, 0, 1, display_height, 0xFFFFFF));
78 }
79 
ZTEST(draw_line,test_draw_line_bottom_end)80 ZTEST(draw_line, test_draw_line_bottom_end)
81 {
82 	struct cfb_position start = {0, 239};
83 	struct cfb_position end = {display_width, 239};
84 
85 	zassert_ok(cfb_draw_line(dev, &start, &end));
86 	zassert_ok(cfb_framebuffer_finalize(dev));
87 
88 	zassert_true(verify_color_inside_rect(0, display_height - 1, display_width, 1, 0xFFFFFF));
89 }
90 
ZTEST(draw_line,test_render_twice_on_same_tile)91 ZTEST(draw_line, test_render_twice_on_same_tile)
92 {
93 	struct cfb_position start = {0, 0};
94 	struct cfb_position end = {display_width, 0};
95 
96 	zassert_ok(cfb_draw_line(dev, &start, &end));
97 	start.y = 7;
98 	end.y = 7;
99 	zassert_ok(cfb_draw_line(dev, &start, &end));
100 	zassert_ok(cfb_framebuffer_finalize(dev));
101 
102 	zassert_true(verify_color_inside_rect(0, 0, display_width, 1, 0xFFFFFF));
103 	zassert_true(verify_color_inside_rect(0, 1, display_width, 6, 0x0));
104 	zassert_true(verify_color_inside_rect(0, 7, display_width, 1, 0xFFFFFF));
105 	zassert_true(verify_color_inside_rect(0, 8, display_width, 232, 0x0));
106 }
107 
ZTEST(draw_line,test_crossing_diagonally_end_to_end)108 ZTEST(draw_line, test_crossing_diagonally_end_to_end)
109 {
110 	struct cfb_position start = {0, 0};
111 	struct cfb_position end = {320, 240};
112 
113 	zassert_ok(cfb_draw_line(dev, &start, &end));
114 	zassert_ok(cfb_framebuffer_finalize(dev));
115 
116 	for (size_t i = 0; i < 10; i++) {
117 		zassert_true(verify_image(32 * i, 24 * i, diagonal3224, 32, 24));
118 	}
119 }
120 
ZTEST(draw_line,test_crossing_diagonally_from_outside_area)121 ZTEST(draw_line, test_crossing_diagonally_from_outside_area)
122 {
123 	struct cfb_position start = {-32, -48};
124 	struct cfb_position end = {384, 264};
125 
126 	zassert_ok(cfb_draw_line(dev, &start, &end));
127 	zassert_ok(cfb_framebuffer_finalize(dev));
128 
129 	for (size_t i = 0; i < 9; i++) {
130 		zassert_true(verify_image(32 + 32 * i, 24 * i, diagonal3224, 32, 24));
131 	}
132 }
133 
134 ZTEST_SUITE(draw_line, NULL, NULL, cfb_test_before, cfb_test_after, NULL);
135