video_bmp.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2015 Google, Inc
  4. */
  5. #include <common.h>
  6. #include <bmp_layout.h>
  7. #include <dm.h>
  8. #include <mapmem.h>
  9. #include <splash.h>
  10. #include <video.h>
  11. #include <watchdog.h>
  12. #include <asm/unaligned.h>
  13. #ifdef CONFIG_VIDEO_BMP_RLE8
  14. #define BMP_RLE8_ESCAPE 0
  15. #define BMP_RLE8_EOL 0
  16. #define BMP_RLE8_EOBMP 1
  17. #define BMP_RLE8_DELTA 2
  18. static void draw_unencoded_bitmap(ushort **fbp, uchar *bmap, ushort *cmap,
  19. int cnt)
  20. {
  21. while (cnt > 0) {
  22. *(*fbp)++ = cmap[*bmap++];
  23. cnt--;
  24. }
  25. }
  26. static void draw_encoded_bitmap(ushort **fbp, ushort col, int cnt)
  27. {
  28. ushort *fb = *fbp;
  29. while (cnt > 0) {
  30. *fb++ = col;
  31. cnt--;
  32. }
  33. *fbp = fb;
  34. }
  35. static void video_display_rle8_bitmap(struct udevice *dev,
  36. struct bmp_image *bmp, ushort *cmap,
  37. uchar *fb, int x_off, int y_off,
  38. ulong width, ulong height)
  39. {
  40. struct video_priv *priv = dev_get_uclass_priv(dev);
  41. uchar *bmap;
  42. ulong cnt, runlen;
  43. int x, y;
  44. int decode = 1;
  45. debug("%s\n", __func__);
  46. bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
  47. x = 0;
  48. y = height - 1;
  49. while (decode) {
  50. if (bmap[0] == BMP_RLE8_ESCAPE) {
  51. switch (bmap[1]) {
  52. case BMP_RLE8_EOL:
  53. /* end of line */
  54. bmap += 2;
  55. x = 0;
  56. y--;
  57. /* 16bpix, 2-byte per pixel, width should *2 */
  58. fb -= (width * 2 + priv->line_length);
  59. break;
  60. case BMP_RLE8_EOBMP:
  61. /* end of bitmap */
  62. decode = 0;
  63. break;
  64. case BMP_RLE8_DELTA:
  65. /* delta run */
  66. x += bmap[2];
  67. y -= bmap[3];
  68. /* 16bpix, 2-byte per pixel, x should *2 */
  69. fb = (uchar *)(priv->fb + (y + y_off - 1)
  70. * priv->line_length + (x + x_off) * 2);
  71. bmap += 4;
  72. break;
  73. default:
  74. /* unencoded run */
  75. runlen = bmap[1];
  76. bmap += 2;
  77. if (y < height) {
  78. if (x < width) {
  79. if (x + runlen > width)
  80. cnt = width - x;
  81. else
  82. cnt = runlen;
  83. draw_unencoded_bitmap(
  84. (ushort **)&fb,
  85. bmap, cmap, cnt);
  86. }
  87. x += runlen;
  88. }
  89. bmap += runlen;
  90. if (runlen & 1)
  91. bmap++;
  92. }
  93. } else {
  94. /* encoded run */
  95. if (y < height) {
  96. runlen = bmap[0];
  97. if (x < width) {
  98. /* aggregate the same code */
  99. while (bmap[0] == 0xff &&
  100. bmap[2] != BMP_RLE8_ESCAPE &&
  101. bmap[1] == bmap[3]) {
  102. runlen += bmap[2];
  103. bmap += 2;
  104. }
  105. if (x + runlen > width)
  106. cnt = width - x;
  107. else
  108. cnt = runlen;
  109. draw_encoded_bitmap((ushort **)&fb,
  110. cmap[bmap[1]], cnt);
  111. }
  112. x += runlen;
  113. }
  114. bmap += 2;
  115. }
  116. }
  117. }
  118. #endif
  119. __weak void fb_put_byte(uchar **fb, uchar **from)
  120. {
  121. *(*fb)++ = *(*from)++;
  122. }
  123. #if defined(CONFIG_BMP_16BPP)
  124. __weak void fb_put_word(uchar **fb, uchar **from)
  125. {
  126. *(*fb)++ = *(*from)++;
  127. *(*fb)++ = *(*from)++;
  128. }
  129. #endif /* CONFIG_BMP_16BPP */
  130. /**
  131. * video_splash_align_axis() - Align a single coordinate
  132. *
  133. *- if a coordinate is 0x7fff then the image will be centred in
  134. * that direction
  135. *- if a coordinate is -ve then it will be offset to the
  136. * left/top of the centre by that many pixels
  137. *- if a coordinate is positive it will be used unchnaged.
  138. *
  139. * @axis: Input and output coordinate
  140. * @panel_size: Size of panel in pixels for that axis
  141. * @picture_size: Size of bitmap in pixels for that axis
  142. */
  143. static void video_splash_align_axis(int *axis, unsigned long panel_size,
  144. unsigned long picture_size)
  145. {
  146. long panel_picture_delta = panel_size - picture_size;
  147. long axis_alignment;
  148. if (*axis == BMP_ALIGN_CENTER)
  149. axis_alignment = panel_picture_delta / 2;
  150. else if (*axis < 0)
  151. axis_alignment = panel_picture_delta + *axis + 1;
  152. else
  153. return;
  154. *axis = max(0, (int)axis_alignment);
  155. }
  156. static void video_set_cmap(struct udevice *dev,
  157. struct bmp_color_table_entry *cte, unsigned colours)
  158. {
  159. struct video_priv *priv = dev_get_uclass_priv(dev);
  160. int i;
  161. ushort *cmap = priv->cmap;
  162. debug("%s: colours=%d\n", __func__, colours);
  163. for (i = 0; i < colours; ++i) {
  164. *cmap = ((cte->red << 8) & 0xf800) |
  165. ((cte->green << 3) & 0x07e0) |
  166. ((cte->blue >> 3) & 0x001f);
  167. cmap++;
  168. cte++;
  169. }
  170. }
  171. int video_bmp_display(struct udevice *dev, ulong bmp_image, int x, int y,
  172. bool align)
  173. {
  174. struct video_priv *priv = dev_get_uclass_priv(dev);
  175. ushort *cmap_base = NULL;
  176. int i, j;
  177. uchar *fb;
  178. struct bmp_image *bmp = map_sysmem(bmp_image, 0);
  179. uchar *bmap;
  180. ushort padded_width;
  181. unsigned long width, height, byte_width;
  182. unsigned long pwidth = priv->xsize;
  183. unsigned colours, bpix, bmp_bpix;
  184. struct bmp_color_table_entry *palette;
  185. int hdr_size;
  186. if (!bmp || !(bmp->header.signature[0] == 'B' &&
  187. bmp->header.signature[1] == 'M')) {
  188. printf("Error: no valid bmp image at %lx\n", bmp_image);
  189. return -EINVAL;
  190. }
  191. width = get_unaligned_le32(&bmp->header.width);
  192. height = get_unaligned_le32(&bmp->header.height);
  193. bmp_bpix = get_unaligned_le16(&bmp->header.bit_count);
  194. hdr_size = get_unaligned_le16(&bmp->header.size);
  195. debug("hdr_size=%d, bmp_bpix=%d\n", hdr_size, bmp_bpix);
  196. palette = (void *)bmp + 14 + hdr_size;
  197. colours = 1 << bmp_bpix;
  198. bpix = VNBITS(priv->bpix);
  199. if (bpix != 1 && bpix != 8 && bpix != 16 && bpix != 32) {
  200. printf("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
  201. bpix, bmp_bpix);
  202. return -EINVAL;
  203. }
  204. /*
  205. * We support displaying 8bpp and 24bpp BMPs on 16bpp LCDs
  206. * and displaying 24bpp BMPs on 32bpp LCDs
  207. */
  208. if (bpix != bmp_bpix &&
  209. !(bmp_bpix == 8 && bpix == 16) &&
  210. !(bmp_bpix == 24 && bpix == 16) &&
  211. !(bmp_bpix == 24 && bpix == 32)) {
  212. printf("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
  213. bpix, get_unaligned_le16(&bmp->header.bit_count));
  214. return -EPERM;
  215. }
  216. debug("Display-bmp: %d x %d with %d colours, display %d\n",
  217. (int)width, (int)height, (int)colours, 1 << bpix);
  218. if (bmp_bpix == 8)
  219. video_set_cmap(dev, palette, colours);
  220. padded_width = (width & 0x3 ? (width & ~0x3) + 4 : width);
  221. if (align) {
  222. video_splash_align_axis(&x, priv->xsize, width);
  223. video_splash_align_axis(&y, priv->ysize, height);
  224. }
  225. if ((x + width) > pwidth)
  226. width = pwidth - x;
  227. if ((y + height) > priv->ysize)
  228. height = priv->ysize - y;
  229. bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
  230. fb = (uchar *)(priv->fb +
  231. (y + height - 1) * priv->line_length + x * bpix / 8);
  232. switch (bmp_bpix) {
  233. case 1:
  234. case 8: {
  235. cmap_base = priv->cmap;
  236. #ifdef CONFIG_VIDEO_BMP_RLE8
  237. u32 compression = get_unaligned_le32(&bmp->header.compression);
  238. debug("compressed %d %d\n", compression, BMP_BI_RLE8);
  239. if (compression == BMP_BI_RLE8) {
  240. if (bpix != 16) {
  241. /* TODO implement render code for bpix != 16 */
  242. printf("Error: only support 16 bpix");
  243. return -EPROTONOSUPPORT;
  244. }
  245. video_display_rle8_bitmap(dev, bmp, cmap_base, fb, x,
  246. y, width, height);
  247. break;
  248. }
  249. #endif
  250. if (bpix != 16)
  251. byte_width = width;
  252. else
  253. byte_width = width * 2;
  254. for (i = 0; i < height; ++i) {
  255. WATCHDOG_RESET();
  256. for (j = 0; j < width; j++) {
  257. if (bpix != 16) {
  258. fb_put_byte(&fb, &bmap);
  259. } else {
  260. *(uint16_t *)fb = cmap_base[*bmap];
  261. bmap++;
  262. fb += sizeof(uint16_t) / sizeof(*fb);
  263. }
  264. }
  265. bmap += (padded_width - width);
  266. fb -= byte_width + priv->line_length;
  267. }
  268. break;
  269. }
  270. #if defined(CONFIG_BMP_16BPP)
  271. case 16:
  272. for (i = 0; i < height; ++i) {
  273. WATCHDOG_RESET();
  274. for (j = 0; j < width; j++)
  275. fb_put_word(&fb, &bmap);
  276. bmap += (padded_width - width) * 2;
  277. fb -= width * 2 + priv->line_length;
  278. }
  279. break;
  280. #endif /* CONFIG_BMP_16BPP */
  281. #if defined(CONFIG_BMP_24BPP)
  282. case 24:
  283. for (i = 0; i < height; ++i) {
  284. for (j = 0; j < width; j++) {
  285. if (bpix == 16) {
  286. /* 16bit 555RGB format */
  287. *(u16 *)fb = ((bmap[2] >> 3) << 10) |
  288. ((bmap[1] >> 3) << 5) |
  289. (bmap[0] >> 3);
  290. bmap += 3;
  291. fb += 2;
  292. } else {
  293. *(fb++) = *(bmap++);
  294. *(fb++) = *(bmap++);
  295. *(fb++) = *(bmap++);
  296. *(fb++) = 0;
  297. }
  298. }
  299. fb -= priv->line_length + width * (bpix / 8);
  300. bmap += (padded_width - width) * 3;
  301. }
  302. break;
  303. #endif /* CONFIG_BMP_24BPP */
  304. #if defined(CONFIG_BMP_32BPP)
  305. case 32:
  306. for (i = 0; i < height; ++i) {
  307. for (j = 0; j < width; j++) {
  308. *(fb++) = *(bmap++);
  309. *(fb++) = *(bmap++);
  310. *(fb++) = *(bmap++);
  311. *(fb++) = *(bmap++);
  312. }
  313. fb -= priv->line_length + width * (bpix / 8);
  314. }
  315. break;
  316. #endif /* CONFIG_BMP_32BPP */
  317. default:
  318. break;
  319. };
  320. video_sync(dev, false);
  321. return 0;
  322. }