video_bmp.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 <log.h>
  9. #include <mapmem.h>
  10. #include <splash.h>
  11. #include <video.h>
  12. #include <watchdog.h>
  13. #include <asm/unaligned.h>
  14. #ifdef CONFIG_VIDEO_BMP_RLE8
  15. #define BMP_RLE8_ESCAPE 0
  16. #define BMP_RLE8_EOL 0
  17. #define BMP_RLE8_EOBMP 1
  18. #define BMP_RLE8_DELTA 2
  19. static void draw_unencoded_bitmap(ushort **fbp, uchar *bmap, ushort *cmap,
  20. int cnt)
  21. {
  22. while (cnt > 0) {
  23. *(*fbp)++ = cmap[*bmap++];
  24. cnt--;
  25. }
  26. }
  27. static void draw_encoded_bitmap(ushort **fbp, ushort col, int cnt)
  28. {
  29. ushort *fb = *fbp;
  30. while (cnt > 0) {
  31. *fb++ = col;
  32. cnt--;
  33. }
  34. *fbp = fb;
  35. }
  36. static void video_display_rle8_bitmap(struct udevice *dev,
  37. struct bmp_image *bmp, ushort *cmap,
  38. uchar *fb, int x_off, int y_off,
  39. ulong width, ulong height)
  40. {
  41. struct video_priv *priv = dev_get_uclass_priv(dev);
  42. uchar *bmap;
  43. ulong cnt, runlen;
  44. int x, y;
  45. int decode = 1;
  46. debug("%s\n", __func__);
  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. /**
  132. * video_splash_align_axis() - Align a single coordinate
  133. *
  134. *- if a coordinate is 0x7fff then the image will be centred in
  135. * that direction
  136. *- if a coordinate is -ve then it will be offset to the
  137. * left/top of the centre by that many pixels
  138. *- if a coordinate is positive it will be used unchnaged.
  139. *
  140. * @axis: Input and output coordinate
  141. * @panel_size: Size of panel in pixels for that axis
  142. * @picture_size: Size of bitmap in pixels for that axis
  143. */
  144. static void video_splash_align_axis(int *axis, unsigned long panel_size,
  145. unsigned long picture_size)
  146. {
  147. long panel_picture_delta = panel_size - picture_size;
  148. long axis_alignment;
  149. if (*axis == BMP_ALIGN_CENTER)
  150. axis_alignment = panel_picture_delta / 2;
  151. else if (*axis < 0)
  152. axis_alignment = panel_picture_delta + *axis + 1;
  153. else
  154. return;
  155. *axis = max(0, (int)axis_alignment);
  156. }
  157. static void video_set_cmap(struct udevice *dev,
  158. struct bmp_color_table_entry *cte, unsigned colours)
  159. {
  160. struct video_priv *priv = dev_get_uclass_priv(dev);
  161. int i;
  162. ushort *cmap = priv->cmap;
  163. debug("%s: colours=%d\n", __func__, colours);
  164. for (i = 0; i < colours; ++i) {
  165. *cmap = ((cte->red << 8) & 0xf800) |
  166. ((cte->green << 3) & 0x07e0) |
  167. ((cte->blue >> 3) & 0x001f);
  168. cmap++;
  169. cte++;
  170. }
  171. }
  172. int video_bmp_display(struct udevice *dev, ulong bmp_image, int x, int y,
  173. bool align)
  174. {
  175. struct video_priv *priv = dev_get_uclass_priv(dev);
  176. ushort *cmap_base = NULL;
  177. int i, j;
  178. uchar *start, *fb;
  179. struct bmp_image *bmp = map_sysmem(bmp_image, 0);
  180. uchar *bmap;
  181. ushort padded_width;
  182. unsigned long width, height, byte_width;
  183. unsigned long pwidth = priv->xsize;
  184. unsigned colours, bpix, bmp_bpix;
  185. struct bmp_color_table_entry *palette;
  186. int hdr_size;
  187. int ret;
  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 and 24bpp 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 == 8 && bpix == 24) &&
  213. !(bmp_bpix == 8 && bpix == 32) &&
  214. !(bmp_bpix == 24 && bpix == 16) &&
  215. !(bmp_bpix == 24 && bpix == 32)) {
  216. printf("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
  217. bpix, get_unaligned_le16(&bmp->header.bit_count));
  218. return -EPERM;
  219. }
  220. debug("Display-bmp: %d x %d with %d colours, display %d\n",
  221. (int)width, (int)height, (int)colours, 1 << bpix);
  222. if (bmp_bpix == 8)
  223. video_set_cmap(dev, palette, colours);
  224. padded_width = (width & 0x3 ? (width & ~0x3) + 4 : width);
  225. if (align) {
  226. video_splash_align_axis(&x, priv->xsize, width);
  227. video_splash_align_axis(&y, priv->ysize, height);
  228. }
  229. if ((x + width) > pwidth)
  230. width = pwidth - x;
  231. if ((y + height) > priv->ysize)
  232. height = priv->ysize - y;
  233. bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
  234. start = (uchar *)(priv->fb +
  235. (y + height) * priv->line_length + x * bpix / 8);
  236. /* Move back to the final line to be drawn */
  237. fb = start - priv->line_length;
  238. switch (bmp_bpix) {
  239. case 1:
  240. case 8: {
  241. struct bmp_color_table_entry *cte;
  242. cmap_base = priv->cmap;
  243. #ifdef CONFIG_VIDEO_BMP_RLE8
  244. u32 compression = get_unaligned_le32(&bmp->header.compression);
  245. debug("compressed %d %d\n", compression, BMP_BI_RLE8);
  246. if (compression == BMP_BI_RLE8) {
  247. if (bpix != 16) {
  248. /* TODO implement render code for bpix != 16 */
  249. printf("Error: only support 16 bpix");
  250. return -EPROTONOSUPPORT;
  251. }
  252. video_display_rle8_bitmap(dev, bmp, cmap_base, fb, x,
  253. y, width, height);
  254. break;
  255. }
  256. #endif
  257. byte_width = width * (bpix / 8);
  258. if (!byte_width)
  259. byte_width = width;
  260. for (i = 0; i < height; ++i) {
  261. WATCHDOG_RESET();
  262. for (j = 0; j < width; j++) {
  263. if (bpix == 8) {
  264. fb_put_byte(&fb, &bmap);
  265. } else if (bpix == 16) {
  266. *(uint16_t *)fb = cmap_base[*bmap];
  267. bmap++;
  268. fb += sizeof(uint16_t) / sizeof(*fb);
  269. } else {
  270. /* Only support big endian */
  271. cte = &palette[*bmap];
  272. bmap++;
  273. if (bpix == 24) {
  274. *(fb++) = cte->red;
  275. *(fb++) = cte->green;
  276. *(fb++) = cte->blue;
  277. } else {
  278. *(fb++) = cte->blue;
  279. *(fb++) = cte->green;
  280. *(fb++) = cte->red;
  281. *(fb++) = 0;
  282. }
  283. }
  284. }
  285. bmap += (padded_width - width);
  286. fb -= byte_width + priv->line_length;
  287. }
  288. break;
  289. }
  290. #if defined(CONFIG_BMP_16BPP)
  291. case 16:
  292. for (i = 0; i < height; ++i) {
  293. WATCHDOG_RESET();
  294. for (j = 0; j < width; j++)
  295. fb_put_word(&fb, &bmap);
  296. bmap += (padded_width - width);
  297. fb -= width * 2 + priv->line_length;
  298. }
  299. break;
  300. #endif /* CONFIG_BMP_16BPP */
  301. #if defined(CONFIG_BMP_24BPP)
  302. case 24:
  303. for (i = 0; i < height; ++i) {
  304. for (j = 0; j < width; j++) {
  305. if (bpix == 16) {
  306. /* 16bit 555RGB format */
  307. *(u16 *)fb = ((bmap[2] >> 3) << 10) |
  308. ((bmap[1] >> 3) << 5) |
  309. (bmap[0] >> 3);
  310. bmap += 3;
  311. fb += 2;
  312. } else {
  313. *(fb++) = *(bmap++);
  314. *(fb++) = *(bmap++);
  315. *(fb++) = *(bmap++);
  316. *(fb++) = 0;
  317. }
  318. }
  319. fb -= priv->line_length + width * (bpix / 8);
  320. bmap += (padded_width - width);
  321. }
  322. break;
  323. #endif /* CONFIG_BMP_24BPP */
  324. #if defined(CONFIG_BMP_32BPP)
  325. case 32:
  326. for (i = 0; i < height; ++i) {
  327. for (j = 0; j < width; j++) {
  328. *(fb++) = *(bmap++);
  329. *(fb++) = *(bmap++);
  330. *(fb++) = *(bmap++);
  331. *(fb++) = *(bmap++);
  332. }
  333. fb -= priv->line_length + width * (bpix / 8);
  334. }
  335. break;
  336. #endif /* CONFIG_BMP_32BPP */
  337. default:
  338. break;
  339. };
  340. /* Find the position of the top left of the image in the framebuffer */
  341. fb = (uchar *)(priv->fb + y * priv->line_length + x * bpix / 8);
  342. ret = video_sync_copy(dev, start, fb);
  343. if (ret)
  344. return log_ret(ret);
  345. return video_sync(dev, false);
  346. }