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