1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2018
4  * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
5  */
6 
7 #include <common.h>
8 #include <display_options.h>
9 #include <dm.h>
10 #include <video_osd.h>
11 #include <asm/test.h>
12 #include <dm/test.h>
13 #include <test/test.h>
14 #include <test/ut.h>
15 
16 #include "../../drivers/video/sandbox_osd.h"
17 
18 const uint memsize = 2 * 10 * 10;
19 
split(u8 * mem,uint size,u8 * text,u8 * colors)20 static void split(u8 *mem, uint size, u8 *text, u8 *colors)
21 {
22 	int i;
23 	u16 *p = (u16 *)mem;
24 
25 	for (i = 0; i < size; i++) {
26 		colors[i] = p[i] % 0x100;
27 		text[i] = p[i] / 0x100;
28 	}
29 }
30 
print_mem(u8 * mem,uint width,uint height)31 static void print_mem(u8 *mem, uint width, uint height)
32 {
33 	const uint memsize = 2 * 10 * 10;
34 	u8 colors[memsize / 2];
35 	u8 text[memsize / 2];
36 	int i;
37 
38 	split(mem, memsize / 2, text, colors);
39 
40 	for (i = 0; i < width * height; i++) {
41 		printf("%c", text[i]);
42 		if (i > 0 && ((i + 1) % width) == 0)
43 			printf("\n");
44 	}
45 
46 	printf("\n");
47 
48 	for (i = 0; i < width * height; i++) {
49 		printf("%c", colors[i]);
50 		if (i > 0 && ((i + 1) % width) == 0)
51 			printf("\n");
52 	}
53 }
54 
dm_test_osd_basics(struct unit_test_state * uts)55 static int dm_test_osd_basics(struct unit_test_state *uts)
56 {
57 	struct udevice *dev;
58 	u8 mem[memsize + 1];
59 	u8 colors[memsize / 2];
60 	u8 text[memsize / 2];
61 	struct video_osd_info info;
62 
63 	ut_assertok(uclass_first_device_err(UCLASS_VIDEO_OSD, &dev));
64 
65 	video_osd_get_info(dev, &info);
66 
67 	ut_asserteq(10, info.width);
68 	ut_asserteq(10, info.height);
69 	ut_asserteq(1, info.major_version);
70 	ut_asserteq(0, info.minor_version);
71 
72 	ut_assertok(sandbox_osd_get_mem(dev, mem, memsize));
73 	split(mem, memsize / 2, text, colors);
74 
75 	ut_asserteq_mem(text,
76 			"          "
77 			"          "
78 			"          "
79 			"          "
80 			"          "
81 			"          "
82 			"          "
83 			"          "
84 			"          "
85 			"          ", memsize / 2);
86 
87 	ut_asserteq_mem(colors,
88 			"kkkkkkkkkk"
89 			"kkkkkkkkkk"
90 			"kkkkkkkkkk"
91 			"kkkkkkkkkk"
92 			"kkkkkkkkkk"
93 			"kkkkkkkkkk"
94 			"kkkkkkkkkk"
95 			"kkkkkkkkkk"
96 			"kkkkkkkkkk"
97 			"kkkkkkkkkk", memsize / 2);
98 
99 	print_mem(mem, 10, 10);
100 
101 	ut_assertok(video_osd_print(dev, 1, 1, COLOR_RED, "Blah"));
102 
103 	ut_assertok(sandbox_osd_get_mem(dev, mem, memsize));
104 	split(mem, memsize / 2, text, colors);
105 
106 	ut_asserteq_mem(text,
107 			"          "
108 			" Blah     "
109 			"          "
110 			"          "
111 			"          "
112 			"          "
113 			"          "
114 			"          "
115 			"          "
116 			"          ", memsize / 2);
117 
118 	ut_asserteq_mem(colors,
119 			"kkkkkkkkkk"
120 			"krrrrkkkkk"
121 			"kkkkkkkkkk"
122 			"kkkkkkkkkk"
123 			"kkkkkkkkkk"
124 			"kkkkkkkkkk"
125 			"kkkkkkkkkk"
126 			"kkkkkkkkkk"
127 			"kkkkkkkkkk"
128 			"kkkkkkkkkk", memsize / 2);
129 
130 	print_mem(mem, 10, 10);
131 
132 	return 0;
133 }
134 
135 DM_TEST(dm_test_osd_basics, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
136 
dm_test_osd_extended(struct unit_test_state * uts)137 static int dm_test_osd_extended(struct unit_test_state *uts)
138 {
139 	struct udevice *dev;
140 	u8 mem[memsize + 1];
141 	u8 colors[memsize / 2];
142 	u8 text[memsize / 2];
143 	struct video_osd_info info;
144 	u16 val;
145 
146 	ut_assertok(uclass_first_device_err(UCLASS_VIDEO_OSD, &dev));
147 
148 	ut_assertok(video_osd_set_size(dev, 20, 5));
149 
150 	video_osd_get_info(dev, &info);
151 
152 	ut_asserteq(20, info.width);
153 	ut_asserteq(5, info.height);
154 	ut_asserteq(1, info.major_version);
155 	ut_asserteq(0, info.minor_version);
156 
157 	ut_assertok(sandbox_osd_get_mem(dev, mem, memsize));
158 	split(mem, memsize / 2, text, colors);
159 
160 	ut_asserteq_mem(text,
161 			"                    "
162 			"                    "
163 			"                    "
164 			"                    "
165 			"                    ", memsize / 2);
166 
167 	ut_asserteq_mem(colors,
168 			"kkkkkkkkkkkkkkkkkkkk"
169 			"kkkkkkkkkkkkkkkkkkkk"
170 			"kkkkkkkkkkkkkkkkkkkk"
171 			"kkkkkkkkkkkkkkkkkkkk"
172 			"kkkkkkkkkkkkkkkkkkkk", memsize / 2);
173 
174 	print_mem(mem, 20, 5);
175 
176 	/* Draw green border */
177 	val = '-' * 0x100 + 'g';
178 	ut_assertok(video_osd_set_mem(dev, 1, 0, (u8 *)&val, 2, 18));
179 	ut_assertok(video_osd_set_mem(dev, 1, 4, (u8 *)&val, 2, 18));
180 	ut_assertok(video_osd_print(dev, 0, 1, COLOR_GREEN, "|"));
181 	ut_assertok(video_osd_print(dev, 0, 2, COLOR_GREEN, "|"));
182 	ut_assertok(video_osd_print(dev, 0, 3, COLOR_GREEN, "|"));
183 	ut_assertok(video_osd_print(dev, 19, 1, COLOR_GREEN, "|"));
184 	ut_assertok(video_osd_print(dev, 19, 2, COLOR_GREEN, "|"));
185 	ut_assertok(video_osd_print(dev, 19, 3, COLOR_GREEN, "|"));
186 	ut_assertok(video_osd_print(dev, 0, 0, COLOR_GREEN, "+"));
187 	ut_assertok(video_osd_print(dev, 19, 0, COLOR_GREEN, "+"));
188 	ut_assertok(video_osd_print(dev, 19, 4, COLOR_GREEN, "+"));
189 	ut_assertok(video_osd_print(dev, 0, 4, COLOR_GREEN, "+"));
190 
191 	/* Add menu caption and entries */
192 	ut_assertok(video_osd_print(dev, 5, 0, COLOR_GREEN, " OSD menu "));
193 	ut_assertok(video_osd_print(dev, 2, 1, COLOR_BLUE, " *  Entry 1"));
194 	ut_assertok(video_osd_print(dev, 2, 2, COLOR_BLUE, "(*) Entry 2"));
195 	ut_assertok(video_osd_print(dev, 2, 3, COLOR_BLUE, " *  Entry 3"));
196 
197 	ut_assertok(sandbox_osd_get_mem(dev, mem, memsize));
198 	split(mem, memsize / 2, text, colors);
199 
200 	print_mem(mem, 20, 5);
201 
202 	ut_asserteq_mem(text,
203 			"+---- OSD menu ----+"
204 			"|  *  Entry 1      |"
205 			"| (*) Entry 2      |"
206 			"|  *  Entry 3      |"
207 			 "+------------------+", memsize / 2);
208 
209 	ut_asserteq_mem(colors,
210 			"gggggggggggggggggggg"
211 			"gkbbbbbbbbbbbkkkkkkg"
212 			"gkbbbbbbbbbbbkkkkkkg"
213 			"gkbbbbbbbbbbbkkkkkkg"
214 			"gggggggggggggggggggg", memsize / 2);
215 
216 	return 0;
217 }
218 
219 DM_TEST(dm_test_osd_extended, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
220