video.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2014 Google, Inc
  4. * Written by Simon Glass <sjg@chromium.org>
  5. */
  6. #include <common.h>
  7. #include <bzlib.h>
  8. #include <dm.h>
  9. #include <log.h>
  10. #include <malloc.h>
  11. #include <mapmem.h>
  12. #include <os.h>
  13. #include <video.h>
  14. #include <video_console.h>
  15. #include <dm/test.h>
  16. #include <dm/uclass-internal.h>
  17. #include <test/test.h>
  18. #include <test/ut.h>
  19. /*
  20. * These tests use the standard sandbox frame buffer, the resolution of which
  21. * is defined in the device tree. This only supports 16bpp so the tests only
  22. * test that code path. It would be possible to adjust this fairly easily,
  23. * by adjusting the bpix value in struct sandbox_sdl_plat. However the code
  24. * in sandbox_sdl_sync() would also need to change to handle the different
  25. * surface depth.
  26. */
  27. /* Basic test of the video uclass */
  28. static int dm_test_video_base(struct unit_test_state *uts)
  29. {
  30. struct video_priv *priv;
  31. struct udevice *dev;
  32. ut_assertok(uclass_get_device(UCLASS_VIDEO, 0, &dev));
  33. ut_asserteq(1366, video_get_xsize(dev));
  34. ut_asserteq(768, video_get_ysize(dev));
  35. priv = dev_get_uclass_priv(dev);
  36. ut_asserteq(priv->fb_size, 1366 * 768 * 2);
  37. return 0;
  38. }
  39. DM_TEST(dm_test_video_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  40. /**
  41. * compress_frame_buffer() - Compress the frame buffer and return its size
  42. *
  43. * We want to write tests which perform operations on the video console and
  44. * check that the frame buffer ends up with the correct contents. But it is
  45. * painful to store 'known good' images for comparison with the frame
  46. * buffer. As an alternative, we can compress the frame buffer and check the
  47. * size of the compressed data. This provides a pretty good level of
  48. * certainty and the resulting tests need only check a single value.
  49. *
  50. * If the copy framebuffer is enabled, this compares it to the main framebuffer
  51. * too.
  52. *
  53. * @uts: Test state
  54. * @dev: Video device
  55. * @return compressed size of the frame buffer, or -ve on error
  56. */
  57. static int compress_frame_buffer(struct unit_test_state *uts,
  58. struct udevice *dev)
  59. {
  60. struct video_priv *priv = dev_get_uclass_priv(dev);
  61. struct video_priv *uc_priv = dev_get_uclass_priv(dev);
  62. uint destlen;
  63. void *dest;
  64. int ret;
  65. destlen = priv->fb_size;
  66. dest = malloc(priv->fb_size);
  67. if (!dest)
  68. return -ENOMEM;
  69. ret = BZ2_bzBuffToBuffCompress(dest, &destlen,
  70. priv->fb, priv->fb_size,
  71. 3, 0, 0);
  72. free(dest);
  73. if (ret)
  74. return ret;
  75. /* Check here that the copy frame buffer is working correctly */
  76. if (IS_ENABLED(CONFIG_VIDEO_COPY)) {
  77. ut_assertf(!memcmp(uc_priv->fb, uc_priv->copy_fb,
  78. uc_priv->fb_size),
  79. "Copy framebuffer does not match fb");
  80. }
  81. return destlen;
  82. }
  83. /*
  84. * Call this function at any point to halt and show the current display. Be
  85. * sure to run the test with the -l flag.
  86. */
  87. static void __maybe_unused see_output(void)
  88. {
  89. video_sync_all();
  90. while (1);
  91. }
  92. /* Select the video console driver to use for a video device */
  93. static int select_vidconsole(struct unit_test_state *uts, const char *drv_name)
  94. {
  95. struct sandbox_sdl_plat *plat;
  96. struct udevice *dev;
  97. ut_assertok(uclass_find_device(UCLASS_VIDEO, 0, &dev));
  98. ut_assert(!device_active(dev));
  99. plat = dev_get_platdata(dev);
  100. plat->vidconsole_drv_name = "vidconsole0";
  101. return 0;
  102. }
  103. /* Test text output works on the video console */
  104. static int dm_test_video_text(struct unit_test_state *uts)
  105. {
  106. struct udevice *dev, *con;
  107. int i;
  108. #define WHITE 0xffff
  109. #define SCROLL_LINES 100
  110. ut_assertok(select_vidconsole(uts, "vidconsole0"));
  111. ut_assertok(uclass_get_device(UCLASS_VIDEO, 0, &dev));
  112. ut_asserteq(46, compress_frame_buffer(uts, dev));
  113. ut_assertok(uclass_get_device(UCLASS_VIDEO_CONSOLE, 0, &con));
  114. vidconsole_putc_xy(con, 0, 0, 'a');
  115. ut_asserteq(79, compress_frame_buffer(uts, dev));
  116. vidconsole_putc_xy(con, 0, 0, ' ');
  117. ut_asserteq(46, compress_frame_buffer(uts, dev));
  118. for (i = 0; i < 20; i++)
  119. vidconsole_putc_xy(con, VID_TO_POS(i * 8), 0, ' ' + i);
  120. ut_asserteq(273, compress_frame_buffer(uts, dev));
  121. vidconsole_set_row(con, 0, WHITE);
  122. ut_asserteq(46, compress_frame_buffer(uts, dev));
  123. for (i = 0; i < 20; i++)
  124. vidconsole_putc_xy(con, VID_TO_POS(i * 8), 0, ' ' + i);
  125. ut_asserteq(273, compress_frame_buffer(uts, dev));
  126. return 0;
  127. }
  128. DM_TEST(dm_test_video_text, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  129. /* Test handling of special characters in the console */
  130. static int dm_test_video_chars(struct unit_test_state *uts)
  131. {
  132. struct udevice *dev, *con;
  133. const char *test_string = "Well\b\b\b\bxhe is\r \n\ta very \amodest \bman\n\t\tand Has much to\b\bto be modest about.";
  134. ut_assertok(select_vidconsole(uts, "vidconsole0"));
  135. ut_assertok(uclass_get_device(UCLASS_VIDEO, 0, &dev));
  136. ut_assertok(uclass_get_device(UCLASS_VIDEO_CONSOLE, 0, &con));
  137. vidconsole_put_string(con, test_string);
  138. ut_asserteq(466, compress_frame_buffer(uts, dev));
  139. return 0;
  140. }
  141. DM_TEST(dm_test_video_chars, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  142. #ifdef CONFIG_VIDEO_ANSI
  143. #define ANSI_ESC "\x1b"
  144. /* Test handling of ANSI escape sequences */
  145. static int dm_test_video_ansi(struct unit_test_state *uts)
  146. {
  147. struct udevice *dev, *con;
  148. ut_assertok(select_vidconsole(uts, "vidconsole0"));
  149. ut_assertok(uclass_get_device(UCLASS_VIDEO, 0, &dev));
  150. ut_assertok(uclass_get_device(UCLASS_VIDEO_CONSOLE, 0, &con));
  151. /* reference clear: */
  152. video_clear(con->parent);
  153. video_sync(con->parent, false);
  154. ut_asserteq(46, compress_frame_buffer(uts, dev));
  155. /* test clear escape sequence: [2J */
  156. vidconsole_put_string(con, "A\tB\tC"ANSI_ESC"[2J");
  157. ut_asserteq(46, compress_frame_buffer(uts, dev));
  158. /* test set-cursor: [%d;%df */
  159. vidconsole_put_string(con, "abc"ANSI_ESC"[2;2fab"ANSI_ESC"[4;4fcd");
  160. ut_asserteq(143, compress_frame_buffer(uts, dev));
  161. /* test colors (30-37 fg color, 40-47 bg color) */
  162. vidconsole_put_string(con, ANSI_ESC"[30;41mfoo"); /* black on red */
  163. vidconsole_put_string(con, ANSI_ESC"[33;44mbar"); /* yellow on blue */
  164. ut_asserteq(272, compress_frame_buffer(uts, dev));
  165. return 0;
  166. }
  167. DM_TEST(dm_test_video_ansi, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  168. #endif
  169. /**
  170. * check_vidconsole_output() - Run a text console test
  171. *
  172. * @uts: Test state
  173. * @rot: Console rotation (0=normal orientation, 1=90 degrees clockwise,
  174. * 2=upside down, 3=90 degree counterclockwise)
  175. * @wrap_size: Expected size of compressed frame buffer for the wrap test
  176. * @scroll_size: Same for the scroll test
  177. * @return 0 on success
  178. */
  179. static int check_vidconsole_output(struct unit_test_state *uts, int rot,
  180. int wrap_size, int scroll_size)
  181. {
  182. struct udevice *dev, *con;
  183. struct sandbox_sdl_plat *plat;
  184. int i;
  185. ut_assertok(uclass_find_device(UCLASS_VIDEO, 0, &dev));
  186. ut_assert(!device_active(dev));
  187. plat = dev_get_platdata(dev);
  188. plat->rot = rot;
  189. ut_assertok(uclass_get_device(UCLASS_VIDEO, 0, &dev));
  190. ut_assertok(uclass_get_device(UCLASS_VIDEO_CONSOLE, 0, &con));
  191. ut_asserteq(46, compress_frame_buffer(uts, dev));
  192. /* Check display wrap */
  193. for (i = 0; i < 120; i++)
  194. vidconsole_put_char(con, 'A' + i % 50);
  195. ut_asserteq(wrap_size, compress_frame_buffer(uts, dev));
  196. /* Check display scrolling */
  197. for (i = 0; i < SCROLL_LINES; i++) {
  198. vidconsole_put_char(con, 'A' + i % 50);
  199. vidconsole_put_char(con, '\n');
  200. }
  201. ut_asserteq(scroll_size, compress_frame_buffer(uts, dev));
  202. /* If we scroll enough, the screen becomes blank again */
  203. for (i = 0; i < SCROLL_LINES; i++)
  204. vidconsole_put_char(con, '\n');
  205. ut_asserteq(46, compress_frame_buffer(uts, dev));
  206. return 0;
  207. }
  208. /* Test text output through the console uclass */
  209. static int dm_test_video_context(struct unit_test_state *uts)
  210. {
  211. ut_assertok(select_vidconsole(uts, "vidconsole0"));
  212. ut_assertok(check_vidconsole_output(uts, 0, 788, 453));
  213. return 0;
  214. }
  215. DM_TEST(dm_test_video_context, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  216. /* Test rotated text output through the console uclass */
  217. static int dm_test_video_rotation1(struct unit_test_state *uts)
  218. {
  219. ut_assertok(check_vidconsole_output(uts, 1, 1112, 680));
  220. return 0;
  221. }
  222. DM_TEST(dm_test_video_rotation1, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  223. /* Test rotated text output through the console uclass */
  224. static int dm_test_video_rotation2(struct unit_test_state *uts)
  225. {
  226. ut_assertok(check_vidconsole_output(uts, 2, 783, 445));
  227. return 0;
  228. }
  229. DM_TEST(dm_test_video_rotation2, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  230. /* Test rotated text output through the console uclass */
  231. static int dm_test_video_rotation3(struct unit_test_state *uts)
  232. {
  233. ut_assertok(check_vidconsole_output(uts, 3, 1134, 681));
  234. return 0;
  235. }
  236. DM_TEST(dm_test_video_rotation3, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  237. /* Read a file into memory and return a pointer to it */
  238. static int read_file(struct unit_test_state *uts, const char *fname,
  239. ulong *addrp)
  240. {
  241. int buf_size = 100000;
  242. ulong addr = 0;
  243. int size, fd;
  244. char *buf;
  245. buf = map_sysmem(addr, 0);
  246. ut_assert(buf != NULL);
  247. fd = os_open(fname, OS_O_RDONLY);
  248. ut_assert(fd >= 0);
  249. size = os_read(fd, buf, buf_size);
  250. os_close(fd);
  251. ut_assert(size >= 0);
  252. ut_assert(size < buf_size);
  253. *addrp = addr;
  254. return 0;
  255. }
  256. /* Test drawing a bitmap file */
  257. static int dm_test_video_bmp(struct unit_test_state *uts)
  258. {
  259. struct udevice *dev;
  260. ulong addr;
  261. ut_assertok(uclass_get_device(UCLASS_VIDEO, 0, &dev));
  262. ut_assertok(read_file(uts, "tools/logos/denx.bmp", &addr));
  263. ut_assertok(video_bmp_display(dev, addr, 0, 0, false));
  264. ut_asserteq(1368, compress_frame_buffer(uts, dev));
  265. return 0;
  266. }
  267. DM_TEST(dm_test_video_bmp, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  268. /* Test drawing a compressed bitmap file */
  269. static int dm_test_video_bmp_comp(struct unit_test_state *uts)
  270. {
  271. struct udevice *dev;
  272. ulong addr;
  273. ut_assertok(uclass_get_device(UCLASS_VIDEO, 0, &dev));
  274. ut_assertok(read_file(uts, "tools/logos/denx-comp.bmp", &addr));
  275. ut_assertok(video_bmp_display(dev, addr, 0, 0, false));
  276. ut_asserteq(1368, compress_frame_buffer(uts, dev));
  277. return 0;
  278. }
  279. DM_TEST(dm_test_video_bmp_comp, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  280. /* Test TrueType console */
  281. static int dm_test_video_truetype(struct unit_test_state *uts)
  282. {
  283. struct udevice *dev, *con;
  284. const char *test_string = "Criticism may not be agreeable, but it is necessary. It fulfils the same function as pain in the human body. It calls attention to an unhealthy state of things. Some see private enterprise as a predatory target to be shot, others as a cow to be milked, but few are those who see it as a sturdy horse pulling the wagon. The \aprice OF\b\bof greatness\n\tis responsibility.\n\nBye";
  285. ut_assertok(uclass_get_device(UCLASS_VIDEO, 0, &dev));
  286. ut_assertok(uclass_get_device(UCLASS_VIDEO_CONSOLE, 0, &con));
  287. vidconsole_put_string(con, test_string);
  288. ut_asserteq(12237, compress_frame_buffer(uts, dev));
  289. return 0;
  290. }
  291. DM_TEST(dm_test_video_truetype, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  292. /* Test scrolling TrueType console */
  293. static int dm_test_video_truetype_scroll(struct unit_test_state *uts)
  294. {
  295. struct sandbox_sdl_plat *plat;
  296. struct udevice *dev, *con;
  297. const char *test_string = "Criticism may not be agreeable, but it is necessary. It fulfils the same function as pain in the human body. It calls attention to an unhealthy state of things. Some see private enterprise as a predatory target to be shot, others as a cow to be milked, but few are those who see it as a sturdy horse pulling the wagon. The \aprice OF\b\bof greatness\n\tis responsibility.\n\nBye";
  298. ut_assertok(uclass_find_device(UCLASS_VIDEO, 0, &dev));
  299. ut_assert(!device_active(dev));
  300. plat = dev_get_platdata(dev);
  301. plat->font_size = 100;
  302. ut_assertok(uclass_get_device(UCLASS_VIDEO, 0, &dev));
  303. ut_assertok(uclass_get_device(UCLASS_VIDEO_CONSOLE, 0, &con));
  304. vidconsole_put_string(con, test_string);
  305. ut_asserteq(35030, compress_frame_buffer(uts, dev));
  306. return 0;
  307. }
  308. DM_TEST(dm_test_video_truetype_scroll, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  309. /* Test TrueType backspace, within and across lines */
  310. static int dm_test_video_truetype_bs(struct unit_test_state *uts)
  311. {
  312. struct sandbox_sdl_plat *plat;
  313. struct udevice *dev, *con;
  314. const char *test_string = "...Criticism may or may\b\b\b\b\b\bnot be agreeable, but seldom it is necessary\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\bit is necessary. It fulfils the same function as pain in the human body. It calls attention to an unhealthy state of things.";
  315. ut_assertok(uclass_find_device(UCLASS_VIDEO, 0, &dev));
  316. ut_assert(!device_active(dev));
  317. plat = dev_get_platdata(dev);
  318. plat->font_size = 100;
  319. ut_assertok(uclass_get_device(UCLASS_VIDEO, 0, &dev));
  320. ut_assertok(uclass_get_device(UCLASS_VIDEO_CONSOLE, 0, &con));
  321. vidconsole_put_string(con, test_string);
  322. ut_asserteq(29018, compress_frame_buffer(uts, dev));
  323. return 0;
  324. }
  325. DM_TEST(dm_test_video_truetype_bs, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);