video_bmp.c 8.0 KB

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