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