console_truetype.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2016 Google, Inc
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <video.h>
  8. #include <video_console.h>
  9. /* Functions needed by stb_truetype.h */
  10. static int tt_floor(double val)
  11. {
  12. if (val < 0)
  13. return (int)(val - 0.999);
  14. return (int)val;
  15. }
  16. static int tt_ceil(double val)
  17. {
  18. if (val < 0)
  19. return (int)val;
  20. return (int)(val + 0.999);
  21. }
  22. static double frac(double val)
  23. {
  24. return val - tt_floor(val);
  25. }
  26. static double tt_fabs(double x)
  27. {
  28. return x < 0 ? -x : x;
  29. }
  30. /*
  31. * Simple square root algorithm. This is from:
  32. * http://stackoverflow.com/questions/1623375/writing-your-own-square-root-function
  33. * Written by Chihung Yu
  34. * Creative Commons license
  35. * http://creativecommons.org/licenses/by-sa/3.0/legalcode
  36. * It has been modified to compile correctly, and for U-Boot style.
  37. */
  38. static double tt_sqrt(double value)
  39. {
  40. double lo = 1.0;
  41. double hi = value;
  42. while (hi - lo > 0.00001) {
  43. double mid = lo + (hi - lo) / 2;
  44. if (mid * mid - value > 0.00001)
  45. hi = mid;
  46. else
  47. lo = mid;
  48. }
  49. return lo;
  50. }
  51. #define STBTT_ifloor tt_floor
  52. #define STBTT_iceil tt_ceil
  53. #define STBTT_fabs tt_fabs
  54. #define STBTT_sqrt tt_sqrt
  55. #define STBTT_malloc(size, u) ((void)(u), malloc(size))
  56. #define STBTT_free(size, u) ((void)(u), free(size))
  57. #define STBTT_assert(x)
  58. #define STBTT_strlen(x) strlen(x)
  59. #define STBTT_memcpy memcpy
  60. #define STBTT_memset memset
  61. #define STB_TRUETYPE_IMPLEMENTATION
  62. #include "stb_truetype.h"
  63. /**
  64. * struct pos_info - Records a cursor position
  65. *
  66. * @xpos_frac: Fractional X position in pixels (multiplied by VID_FRAC_DIV)
  67. * @ypos: Y position (pixels from the top)
  68. */
  69. struct pos_info {
  70. int xpos_frac;
  71. int ypos;
  72. };
  73. /*
  74. * Allow one for each character on the command line plus one for each newline.
  75. * This is just an estimate, but it should not be exceeded.
  76. */
  77. #define POS_HISTORY_SIZE (CONFIG_SYS_CBSIZE * 11 / 10)
  78. /**
  79. * struct console_tt_priv - Private data for this driver
  80. *
  81. * @font_size: Vertical font size in pixels
  82. * @font_data: Pointer to TrueType font file contents
  83. * @font: TrueType font information for the current font
  84. * @pos: List of cursor positions for each character written. This is
  85. * used to handle backspace. We clear the frame buffer between
  86. * the last position and the current position, thus erasing the
  87. * last character. We record enough characters to go back to the
  88. * start of the current command line.
  89. * @pos_ptr: Current position in the position history
  90. * @baseline: Pixel offset of the font's baseline from the cursor position.
  91. * This is the 'ascent' of the font, scaled to pixel coordinates.
  92. * It measures the distance from the baseline to the top of the
  93. * font.
  94. * @scale: Scale of the font. This is calculated from the pixel height
  95. * of the font. It is used by the STB library to generate images
  96. * of the correct size.
  97. */
  98. struct console_tt_priv {
  99. int font_size;
  100. u8 *font_data;
  101. stbtt_fontinfo font;
  102. struct pos_info pos[POS_HISTORY_SIZE];
  103. int pos_ptr;
  104. int baseline;
  105. double scale;
  106. };
  107. static int console_truetype_set_row(struct udevice *dev, uint row, int clr)
  108. {
  109. struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
  110. struct console_tt_priv *priv = dev_get_priv(dev);
  111. void *line;
  112. int pixels = priv->font_size * vid_priv->line_length;
  113. int i;
  114. line = vid_priv->fb + row * priv->font_size * vid_priv->line_length;
  115. switch (vid_priv->bpix) {
  116. #ifdef CONFIG_VIDEO_BPP8
  117. case VIDEO_BPP8: {
  118. uint8_t *dst = line;
  119. for (i = 0; i < pixels; i++)
  120. *dst++ = clr;
  121. break;
  122. }
  123. #endif
  124. #ifdef CONFIG_VIDEO_BPP16
  125. case VIDEO_BPP16: {
  126. uint16_t *dst = line;
  127. for (i = 0; i < pixels; i++)
  128. *dst++ = clr;
  129. break;
  130. }
  131. #endif
  132. #ifdef CONFIG_VIDEO_BPP32
  133. case VIDEO_BPP32: {
  134. uint32_t *dst = line;
  135. for (i = 0; i < pixels; i++)
  136. *dst++ = clr;
  137. break;
  138. }
  139. #endif
  140. default:
  141. return -ENOSYS;
  142. }
  143. return 0;
  144. }
  145. static int console_truetype_move_rows(struct udevice *dev, uint rowdst,
  146. uint rowsrc, uint count)
  147. {
  148. struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
  149. struct console_tt_priv *priv = dev_get_priv(dev);
  150. void *dst;
  151. void *src;
  152. int i, diff;
  153. dst = vid_priv->fb + rowdst * priv->font_size * vid_priv->line_length;
  154. src = vid_priv->fb + rowsrc * priv->font_size * vid_priv->line_length;
  155. memmove(dst, src, priv->font_size * vid_priv->line_length * count);
  156. /* Scroll up our position history */
  157. diff = (rowsrc - rowdst) * priv->font_size;
  158. for (i = 0; i < priv->pos_ptr; i++)
  159. priv->pos[i].ypos -= diff;
  160. return 0;
  161. }
  162. static int console_truetype_putc_xy(struct udevice *dev, uint x, uint y,
  163. char ch)
  164. {
  165. struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev);
  166. struct udevice *vid = dev->parent;
  167. struct video_priv *vid_priv = dev_get_uclass_priv(vid);
  168. struct console_tt_priv *priv = dev_get_priv(dev);
  169. stbtt_fontinfo *font = &priv->font;
  170. int width, height, xoff, yoff;
  171. double xpos, x_shift;
  172. int lsb;
  173. int width_frac, linenum;
  174. struct pos_info *pos;
  175. u8 *bits, *data;
  176. int advance;
  177. void *line;
  178. int row;
  179. /* First get some basic metrics about this character */
  180. stbtt_GetCodepointHMetrics(font, ch, &advance, &lsb);
  181. /*
  182. * First out our current X position in fractional pixels. If we wrote
  183. * a character previously, using kerning to fine-tune the position of
  184. * this character */
  185. xpos = frac(VID_TO_PIXEL((double)x));
  186. if (vc_priv->last_ch) {
  187. xpos += priv->scale * stbtt_GetCodepointKernAdvance(font,
  188. vc_priv->last_ch, ch);
  189. }
  190. /*
  191. * Figure out where the cursor will move to after this character, and
  192. * abort if we are out of space on this line. Also calculate the
  193. * effective width of this character, which will be our return value:
  194. * it dictates how much the cursor will move forward on the line.
  195. */
  196. x_shift = xpos - (double)tt_floor(xpos);
  197. xpos += advance * priv->scale;
  198. width_frac = (int)VID_TO_POS(xpos);
  199. if (x + width_frac >= vc_priv->xsize_frac)
  200. return -EAGAIN;
  201. /* Write the current cursor position into history */
  202. if (priv->pos_ptr < POS_HISTORY_SIZE) {
  203. pos = &priv->pos[priv->pos_ptr];
  204. pos->xpos_frac = vc_priv->xcur_frac;
  205. pos->ypos = vc_priv->ycur;
  206. priv->pos_ptr++;
  207. }
  208. /*
  209. * Figure out how much past the start of a pixel we are, and pass this
  210. * information into the render, which will return a 8-bit-per-pixel
  211. * image of the character. For empty characters, like ' ', data will
  212. * return NULL;
  213. */
  214. data = stbtt_GetCodepointBitmapSubpixel(font, priv->scale, priv->scale,
  215. x_shift, 0, ch, &width, &height,
  216. &xoff, &yoff);
  217. if (!data)
  218. return width_frac;
  219. /* Figure out where to write the character in the frame buffer */
  220. bits = data;
  221. line = vid_priv->fb + y * vid_priv->line_length +
  222. VID_TO_PIXEL(x) * VNBYTES(vid_priv->bpix);
  223. linenum = priv->baseline + yoff;
  224. if (linenum > 0)
  225. line += linenum * vid_priv->line_length;
  226. /*
  227. * Write a row at a time, converting the 8bpp image into the colour
  228. * depth of the display. We only expect white-on-black or the reverse
  229. * so the code only handles this simple case.
  230. */
  231. for (row = 0; row < height; row++) {
  232. switch (vid_priv->bpix) {
  233. #ifdef CONFIG_VIDEO_BPP16
  234. case VIDEO_BPP16: {
  235. uint16_t *dst = (uint16_t *)line + xoff;
  236. int i;
  237. for (i = 0; i < width; i++) {
  238. int val = *bits;
  239. int out;
  240. if (vid_priv->colour_bg)
  241. val = 255 - val;
  242. out = val >> 3 |
  243. (val >> 2) << 5 |
  244. (val >> 3) << 11;
  245. if (vid_priv->colour_fg)
  246. *dst++ |= out;
  247. else
  248. *dst++ &= out;
  249. bits++;
  250. }
  251. break;
  252. }
  253. #endif
  254. default:
  255. free(data);
  256. return -ENOSYS;
  257. }
  258. line += vid_priv->line_length;
  259. }
  260. free(data);
  261. return width_frac;
  262. }
  263. /**
  264. * console_truetype_erase() - Erase a character
  265. *
  266. * This is used for backspace. We erase a square of the display within the
  267. * given bounds.
  268. *
  269. * @dev: Device to update
  270. * @xstart: X start position in pixels from the left
  271. * @ystart: Y start position in pixels from the top
  272. * @xend: X end position in pixels from the left
  273. * @yend: Y end position in pixels from the top
  274. * @clr: Value to write
  275. * @return 0 if OK, -ENOSYS if the display depth is not supported
  276. */
  277. static int console_truetype_erase(struct udevice *dev, int xstart, int ystart,
  278. int xend, int yend, int clr)
  279. {
  280. struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
  281. void *line;
  282. int pixels = xend - xstart;
  283. int row, i;
  284. line = vid_priv->fb + ystart * vid_priv->line_length;
  285. line += xstart * VNBYTES(vid_priv->bpix);
  286. for (row = ystart; row < yend; row++) {
  287. switch (vid_priv->bpix) {
  288. #ifdef CONFIG_VIDEO_BPP8
  289. case VIDEO_BPP8: {
  290. uint8_t *dst = line;
  291. for (i = 0; i < pixels; i++)
  292. *dst++ = clr;
  293. break;
  294. }
  295. #endif
  296. #ifdef CONFIG_VIDEO_BPP16
  297. case VIDEO_BPP16: {
  298. uint16_t *dst = line;
  299. for (i = 0; i < pixels; i++)
  300. *dst++ = clr;
  301. break;
  302. }
  303. #endif
  304. #ifdef CONFIG_VIDEO_BPP32
  305. case VIDEO_BPP32: {
  306. uint32_t *dst = line;
  307. for (i = 0; i < pixels; i++)
  308. *dst++ = clr;
  309. break;
  310. }
  311. #endif
  312. default:
  313. return -ENOSYS;
  314. }
  315. line += vid_priv->line_length;
  316. }
  317. return 0;
  318. }
  319. /**
  320. * console_truetype_backspace() - Handle a backspace operation
  321. *
  322. * This clears the previous character so that the console looks as if it had
  323. * not been entered.
  324. *
  325. * @dev: Device to update
  326. * @return 0 if OK, -ENOSYS if not supported
  327. */
  328. static int console_truetype_backspace(struct udevice *dev)
  329. {
  330. struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev);
  331. struct console_tt_priv *priv = dev_get_priv(dev);
  332. struct udevice *vid_dev = dev->parent;
  333. struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
  334. struct pos_info *pos;
  335. int xend;
  336. /*
  337. * This indicates a very strange error higher in the stack. The caller
  338. * has sent out n character and n + 1 backspaces.
  339. */
  340. if (!priv->pos_ptr)
  341. return -ENOSYS;
  342. /* Pop the last cursor position off the stack */
  343. pos = &priv->pos[--priv->pos_ptr];
  344. /*
  345. * Figure out the end position for clearing. Normlly it is the current
  346. * cursor position, but if we are clearing a character on the previous
  347. * line, we clear from the end of the line.
  348. */
  349. if (pos->ypos == vc_priv->ycur)
  350. xend = VID_TO_PIXEL(vc_priv->xcur_frac);
  351. else
  352. xend = vid_priv->xsize;
  353. console_truetype_erase(dev, VID_TO_PIXEL(pos->xpos_frac), pos->ypos,
  354. xend, pos->ypos + vc_priv->y_charsize,
  355. vid_priv->colour_bg);
  356. /* Move the cursor back to where it was when we pushed this record */
  357. vc_priv->xcur_frac = pos->xpos_frac;
  358. vc_priv->ycur = pos->ypos;
  359. return 0;
  360. }
  361. static int console_truetype_entry_start(struct udevice *dev)
  362. {
  363. struct console_tt_priv *priv = dev_get_priv(dev);
  364. /* A new input line has start, so clear our history */
  365. priv->pos_ptr = 0;
  366. return 0;
  367. }
  368. /*
  369. * Provides a list of fonts which can be obtained at run-time in U-Boot. These
  370. * are compiled in by the Makefile.
  371. *
  372. * At present there is no mechanism to select a particular font - the first
  373. * one found is the one that is used. But the build system and the code here
  374. * supports multiple fonts, which may be useful for certain firmware screens.
  375. */
  376. struct font_info {
  377. char *name;
  378. u8 *begin;
  379. u8 *end;
  380. };
  381. #define FONT_DECL(_name) \
  382. extern u8 __ttf_ ## _name ## _begin[]; \
  383. extern u8 __ttf_ ## _name ## _end[];
  384. #define FONT_ENTRY(_name) { \
  385. .name = #_name, \
  386. .begin = __ttf_ ## _name ## _begin, \
  387. .end = __ttf_ ## _name ## _end, \
  388. }
  389. FONT_DECL(nimbus_sans_l_regular);
  390. FONT_DECL(ankacoder_c75_r);
  391. FONT_DECL(rufscript010);
  392. FONT_DECL(cantoraone_regular);
  393. static struct font_info font_table[] = {
  394. #ifdef CONFIG_CONSOLE_TRUETYPE_NIMBUS
  395. FONT_ENTRY(nimbus_sans_l_regular),
  396. #endif
  397. #ifdef CONFIG_CONSOLE_TRUETYPE_ANKACODER
  398. FONT_ENTRY(ankacoder_c75_r),
  399. #endif
  400. #ifdef CONFIG_CONSOLE_TRUETYPE_RUFSCRIPT
  401. FONT_ENTRY(rufscript010),
  402. #endif
  403. #ifdef CONFIG_CONSOLE_TRUETYPE_CANTORAONE
  404. FONT_ENTRY(cantoraone_regular),
  405. #endif
  406. {} /* sentinel */
  407. };
  408. #define FONT_BEGIN(name) __ttf_ ## name ## _begin
  409. #define FONT_END(name) __ttf_ ## name ## _end
  410. #define FONT_IS_VALID(name) (abs(FONT_END(name) - FONT_BEGIN) > 4)
  411. /**
  412. * console_truetype_find_font() - Find a suitable font
  413. *
  414. * This searched for the first available font.
  415. *
  416. * @return pointer to the font, or NULL if none is found
  417. */
  418. static u8 *console_truetype_find_font(void)
  419. {
  420. struct font_info *tab;
  421. for (tab = font_table; tab->begin; tab++) {
  422. if (abs(tab->begin - tab->end) > 4) {
  423. debug("%s: Font '%s', at %p, size %lx\n", __func__,
  424. tab->name, tab->begin,
  425. (ulong)(tab->end - tab->begin));
  426. return tab->begin;
  427. }
  428. }
  429. return NULL;
  430. }
  431. static int console_truetype_probe(struct udevice *dev)
  432. {
  433. struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev);
  434. struct console_tt_priv *priv = dev_get_priv(dev);
  435. struct udevice *vid_dev = dev->parent;
  436. struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
  437. stbtt_fontinfo *font = &priv->font;
  438. int ascent;
  439. debug("%s: start\n", __func__);
  440. if (vid_priv->font_size)
  441. priv->font_size = vid_priv->font_size;
  442. else
  443. priv->font_size = CONFIG_CONSOLE_TRUETYPE_SIZE;
  444. priv->font_data = console_truetype_find_font();
  445. if (!priv->font_data) {
  446. debug("%s: Could not find any fonts\n", __func__);
  447. return -EBFONT;
  448. }
  449. vc_priv->x_charsize = priv->font_size;
  450. vc_priv->y_charsize = priv->font_size;
  451. vc_priv->xstart_frac = VID_TO_POS(2);
  452. vc_priv->cols = vid_priv->xsize / priv->font_size;
  453. vc_priv->rows = vid_priv->ysize / priv->font_size;
  454. vc_priv->tab_width_frac = VID_TO_POS(priv->font_size) * 8 / 2;
  455. if (!stbtt_InitFont(font, priv->font_data, 0)) {
  456. debug("%s: Font init failed\n", __func__);
  457. return -EPERM;
  458. }
  459. /* Pre-calculate some things we will need regularly */
  460. priv->scale = stbtt_ScaleForPixelHeight(font, priv->font_size);
  461. stbtt_GetFontVMetrics(font, &ascent, 0, 0);
  462. priv->baseline = (int)(ascent * priv->scale);
  463. debug("%s: ready\n", __func__);
  464. return 0;
  465. }
  466. struct vidconsole_ops console_truetype_ops = {
  467. .putc_xy = console_truetype_putc_xy,
  468. .move_rows = console_truetype_move_rows,
  469. .set_row = console_truetype_set_row,
  470. .backspace = console_truetype_backspace,
  471. .entry_start = console_truetype_entry_start,
  472. };
  473. U_BOOT_DRIVER(vidconsole_truetype) = {
  474. .name = "vidconsole_tt",
  475. .id = UCLASS_VIDEO_CONSOLE,
  476. .ops = &console_truetype_ops,
  477. .probe = console_truetype_probe,
  478. .priv_auto_alloc_size = sizeof(struct console_tt_priv),
  479. };