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_rect, 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_rect,test_draw_rect_1123_at_0_0)50 ZTEST(draw_rect, test_draw_rect_1123_at_0_0)
51 {
52 	struct cfb_position start = {0, 0};
53 	struct cfb_position end = {start.x + 10, start.y + 22};
54 
55 	zassert_ok(cfb_draw_rect(dev, &start, &end), "");
56 	zassert_ok(cfb_framebuffer_finalize(dev), "");
57 
58 	zassert_true(verify_image_and_bg(0, 0, rectspace1123, 11, 23, 0), "");
59 }
60 
ZTEST(draw_rect,test_draw_rect_1123_at_1_1)61 ZTEST(draw_rect, test_draw_rect_1123_at_1_1)
62 {
63 	struct cfb_position start = {1, 1};
64 	struct cfb_position end = {start.x + 10, start.y + 22};
65 
66 	zassert_ok(cfb_draw_rect(dev, &start, &end), "");
67 	zassert_ok(cfb_framebuffer_finalize(dev), "");
68 
69 	zassert_true(verify_image_and_bg(1, 1, rectspace1123, 11, 23, 0), "");
70 }
71 
72 /* tile border case */
ZTEST(draw_rect,test_draw_rect_1123_at_9_15)73 ZTEST(draw_rect, test_draw_rect_1123_at_9_15)
74 {
75 	struct cfb_position start = {9, 15};
76 	struct cfb_position end = {start.x + 10, start.y + 22};
77 
78 	zassert_ok(cfb_draw_rect(dev, &start, &end), "");
79 	zassert_ok(cfb_framebuffer_finalize(dev), "");
80 
81 	zassert_true(verify_image_and_bg(9, 15, rectspace1123, 11, 23, 0), "");
82 }
83 
ZTEST(draw_rect,test_draw_rect_1123_at_10_16)84 ZTEST(draw_rect, test_draw_rect_1123_at_10_16)
85 {
86 	struct cfb_position start = {10, 16};
87 	struct cfb_position end = {start.x + 10, start.y + 22};
88 
89 	zassert_ok(cfb_draw_rect(dev, &start, &end), "");
90 	zassert_ok(cfb_framebuffer_finalize(dev), "");
91 
92 	zassert_true(verify_image_and_bg(10, 16, rectspace1123, 11, 23, 0), "");
93 }
94 
ZTEST(draw_rect,test_draw_rect_1123_at_11_17)95 ZTEST(draw_rect, test_draw_rect_1123_at_11_17)
96 {
97 	struct cfb_position start = {11, 17};
98 	struct cfb_position end = {start.x + 10, start.y + 22};
99 
100 	zassert_ok(cfb_draw_rect(dev, &start, &end), "");
101 	zassert_ok(cfb_framebuffer_finalize(dev), "");
102 
103 	zassert_true(verify_image_and_bg(11, 17, rectspace1123, 11, 23, 0), "");
104 }
105 
106 /*
107  * Case of including coordinates outside the area
108  */
ZTEST(draw_rect,test_draw_rect_1123_outside_top_left)109 ZTEST(draw_rect, test_draw_rect_1123_outside_top_left)
110 {
111 	struct cfb_position start = {-(11 - 3), -(23 - 4)};
112 	struct cfb_position end = {start.x + 10, start.y + 22};
113 
114 	zassert_ok(cfb_draw_rect(dev, &start, &end), "");
115 	zassert_ok(cfb_framebuffer_finalize(dev), "");
116 
117 	zassert_true(verify_image_and_bg(0, 0, outside_top_left, 3, 4, 0), "");
118 }
119 
ZTEST(draw_rect,test_draw_rect_1123_outside_top_right)120 ZTEST(draw_rect, test_draw_rect_1123_outside_top_right)
121 {
122 	struct cfb_position start = {display_width - 5, -(23 - 8)};
123 	struct cfb_position end = {start.x + 10, start.y + 22};
124 
125 	zassert_ok(cfb_draw_rect(dev, &start, &end), "");
126 	zassert_ok(cfb_framebuffer_finalize(dev), "");
127 
128 	zassert_true(verify_image(display_width - 5, 0, outside_top_right, 5, 8), "");
129 }
130 
ZTEST(draw_rect,test_draw_rect_1123_outside_bottom_right)131 ZTEST(draw_rect, test_draw_rect_1123_outside_bottom_right)
132 {
133 	struct cfb_position start = {display_width - 3, display_height - 5};
134 	struct cfb_position end = {start.x + 10, start.y + 22};
135 
136 	zassert_ok(cfb_draw_rect(dev, &start, &end), "");
137 	zassert_ok(cfb_framebuffer_finalize(dev), "");
138 
139 	zassert_true(
140 		verify_image(display_width - 3, display_height - 5, outside_bottom_right, 3, 5),
141 		"");
142 }
143 
ZTEST(draw_rect,test_draw_rect_1123_outside_bottom_left)144 ZTEST(draw_rect, test_draw_rect_1123_outside_bottom_left)
145 {
146 	struct cfb_position start = {-(11 - 3), display_height - 14};
147 	struct cfb_position end = {start.x + 10, start.y + 22};
148 
149 	zassert_ok(cfb_draw_rect(dev, &start, &end), "");
150 	zassert_ok(cfb_framebuffer_finalize(dev), "");
151 
152 	zassert_true(verify_image(0, display_height - 14, outside_bottom_left, 3, 14), "");
153 }
154 
155 ZTEST_SUITE(draw_rect, NULL, NULL, cfb_test_before, cfb_test_after, NULL);
156