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_point, 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 
47 /*
48  * normal rendering
49  */
ZTEST(draw_point,test_draw_point_at_0_0)50 ZTEST(draw_point, test_draw_point_at_0_0)
51 {
52 	struct cfb_position pos = {0, 0};
53 
54 	zassert_ok(cfb_draw_point(dev, &pos));
55 	zassert_ok(cfb_framebuffer_finalize(dev));
56 
57 	zassert_true(verify_pixel_and_bg(0, 0, 1, 0));
58 }
59 
ZTEST(draw_point,test_draw_point_at_1_1)60 ZTEST(draw_point, test_draw_point_at_1_1)
61 {
62 	struct cfb_position pos = {1, 1};
63 
64 	zassert_ok(cfb_draw_point(dev, &pos));
65 	zassert_ok(cfb_framebuffer_finalize(dev));
66 
67 	zassert_true(verify_pixel_and_bg(1, 1, 1, 0));
68 }
69 
70 /*
71  * around tile border
72  */
ZTEST(draw_point,test_draw_pont_at_9_15)73 ZTEST(draw_point, test_draw_pont_at_9_15)
74 {
75 	struct cfb_position pos = {9, 15};
76 
77 	zassert_ok(cfb_draw_point(dev, &pos));
78 	zassert_ok(cfb_framebuffer_finalize(dev));
79 
80 	zassert_true(verify_pixel_and_bg(9, 15, 1, 0));
81 }
82 
ZTEST(draw_point,test_draw_point_at_10_16)83 ZTEST(draw_point, test_draw_point_at_10_16)
84 {
85 	struct cfb_position pos = {10, 16};
86 
87 	zassert_ok(cfb_draw_point(dev, &pos));
88 	zassert_ok(cfb_framebuffer_finalize(dev));
89 
90 	zassert_true(verify_pixel_and_bg(10, 16, 1, 0));
91 }
92 
ZTEST(draw_point,test_draw_point_at_11_17)93 ZTEST(draw_point, test_draw_point_at_11_17)
94 {
95 	struct cfb_position pos = {11, 17};
96 
97 	zassert_ok(cfb_draw_point(dev, &pos));
98 	zassert_ok(cfb_framebuffer_finalize(dev));
99 
100 	zassert_true(verify_pixel_and_bg(11, 17, 1, 0));
101 }
102 
ZTEST(draw_point,test_draw_point_twice_on_same_tile)103 ZTEST(draw_point, test_draw_point_twice_on_same_tile)
104 {
105 	struct cfb_position pos = {10, 0};
106 
107 	pos.y = 7;
108 	zassert_ok(cfb_draw_point(dev, &pos));
109 
110 	pos.y = 8;
111 	zassert_ok(cfb_draw_point(dev, &pos));
112 
113 	pos.y = 9;
114 	zassert_ok(cfb_draw_point(dev, &pos));
115 
116 	zassert_ok(cfb_framebuffer_finalize(dev));
117 
118 	zassert_true(verify_color_inside_rect(10, 7, 1, 3, 0xFFFFFF));
119 	zassert_true(verify_color_outside_rect(10, 7, 1, 3, 0x0));
120 }
121 
ZTEST(draw_point,test_draw_point_outside_top_left)122 ZTEST(draw_point, test_draw_point_outside_top_left)
123 {
124 	struct cfb_position pos = {0, -1};
125 
126 	zassert_ok(cfb_draw_point(dev, &pos));
127 	zassert_ok(cfb_framebuffer_finalize(dev));
128 
129 	zassert_true(verify_color_inside_rect(0, 0, display_width, display_height, 0));
130 }
131 
ZTEST(draw_point,test_draw_point_outside_top_right)132 ZTEST(draw_point, test_draw_point_outside_top_right)
133 {
134 	struct cfb_position pos = {display_width, 0};
135 
136 	zassert_ok(cfb_draw_point(dev, &pos));
137 	zassert_ok(cfb_framebuffer_finalize(dev));
138 
139 	zassert_true(verify_color_inside_rect(0, 0, display_width, display_height, 0));
140 }
141 
ZTEST(draw_point,test_draw_point_outside_bottom_right)142 ZTEST(draw_point, test_draw_point_outside_bottom_right)
143 {
144 	struct cfb_position pos = {0, display_height};
145 
146 	zassert_ok(cfb_draw_point(dev, &pos));
147 	zassert_ok(cfb_framebuffer_finalize(dev));
148 
149 	zassert_true(verify_color_inside_rect(0, 0, display_width, display_height, 0));
150 }
151 
ZTEST(draw_point,test_draw_point_outside_bottom_left)152 ZTEST(draw_point, test_draw_point_outside_bottom_left)
153 {
154 	struct cfb_position pos = {-1, display_height};
155 
156 	zassert_ok(cfb_draw_point(dev, &pos));
157 	zassert_ok(cfb_framebuffer_finalize(dev));
158 
159 	zassert_true(verify_color_inside_rect(0, 0, display_width, display_height, 0));
160 }
161 
162 ZTEST_SUITE(draw_point, NULL, NULL, cfb_test_before, cfb_test_after, NULL);
163