video_bmp.c 8.0 KB

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