blob: 45f003c8251ae01861ecb812f83cc94774d110bf [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassb01c7922016-01-18 19:52:22 -07002/*
3 * Copyright (c) 2015 Google, Inc
Simon Glassb01c7922016-01-18 19:52:22 -07004 */
5
6#include <common.h>
7#include <bmp_layout.h>
8#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06009#include <log.h>
Simon Glassb01c7922016-01-18 19:52:22 -070010#include <mapmem.h>
Anatolij Gustschin96d82f62018-12-01 15:30:08 +010011#include <splash.h>
Simon Glassb01c7922016-01-18 19:52:22 -070012#include <video.h>
13#include <watchdog.h>
14#include <asm/unaligned.h>
15
Simon Glassb01c7922016-01-18 19:52:22 -070016#define BMP_RLE8_ESCAPE 0
17#define BMP_RLE8_EOL 0
18#define BMP_RLE8_EOBMP 1
19#define BMP_RLE8_DELTA 2
20
Simon Glass51f92c12021-11-19 13:23:54 -070021/**
22 * get_bmp_col_16bpp() - Convert a colour-table entry into a 16bpp pixel value
23 *
Heinrich Schuchardt185f8122022-01-19 18:05:50 +010024 * Return: value to write to the 16bpp frame buffer for this palette entry
Simon Glass51f92c12021-11-19 13:23:54 -070025 */
26static uint get_bmp_col_16bpp(struct bmp_color_table_entry cte)
27{
28 return ((cte.red << 8) & 0xf800) |
29 ((cte.green << 3) & 0x07e0) |
30 ((cte.blue >> 3) & 0x001f);
31}
32
33/**
Janne Grunau515a2f72022-02-09 22:16:22 +010034 * get_bmp_col_x2r10g10b10() - Convert a colour-table entry into a x2r10g10b10 pixel value
35 *
36 * Return: value to write to the x2r10g10b10 frame buffer for this palette entry
37 */
38static u32 get_bmp_col_x2r10g10b10(struct bmp_color_table_entry *cte)
39{
40 return ((cte->red << 22U) |
41 (cte->green << 12U) |
42 (cte->blue << 2U));
43}
44
45/**
Michal Simekf6de01d2023-05-17 10:42:08 +020046 * get_bmp_col_rgba8888() - Convert a colour-table entry into a rgba8888 pixel value
47 *
48 * Return: value to write to the rgba8888 frame buffer for this palette entry
49 */
50static u32 get_bmp_col_rgba8888(struct bmp_color_table_entry *cte)
51{
52 return ((cte->red) |
53 (cte->green << 8U) |
54 (cte->blue << 16U) | 0xff << 24U);
55}
56
57/**
Simon Glass51f92c12021-11-19 13:23:54 -070058 * write_pix8() - Write a pixel from a BMP image into the framebuffer
59 *
60 * This handles frame buffers with 8, 16, 24 or 32 bits per pixel
61 *
62 * @fb: Place in frame buffer to update
63 * @bpix: Frame buffer bits-per-pixel, which controls how many bytes are written
64 * @palette: BMP palette table
65 * @bmap: Pointer to BMP bitmap position to write. This contains a single byte
66 * which is either written directly (bpix == 8) or used to look up the
67 * palette to get a colour to write
68 */
Janne Grunau515a2f72022-02-09 22:16:22 +010069static void write_pix8(u8 *fb, uint bpix, enum video_format eformat,
70 struct bmp_color_table_entry *palette, u8 *bmap)
Simon Glass51f92c12021-11-19 13:23:54 -070071{
72 if (bpix == 8) {
73 *fb++ = *bmap;
74 } else if (bpix == 16) {
75 *(u16 *)fb = get_bmp_col_16bpp(palette[*bmap]);
76 } else {
77 /* Only support big endian */
78 struct bmp_color_table_entry *cte = &palette[*bmap];
79
80 if (bpix == 24) {
81 *fb++ = cte->red;
82 *fb++ = cte->green;
83 *fb++ = cte->blue;
Janne Grunau515a2f72022-02-09 22:16:22 +010084 } else if (eformat == VIDEO_X2R10G10B10) {
85 *(u32 *)fb = get_bmp_col_x2r10g10b10(cte);
Michal Simekf6de01d2023-05-17 10:42:08 +020086 } else if (eformat == VIDEO_RGBA8888) {
87 *(u32 *)fb = get_bmp_col_rgba8888(cte);
Simon Glass51f92c12021-11-19 13:23:54 -070088 } else {
89 *fb++ = cte->blue;
90 *fb++ = cte->green;
91 *fb++ = cte->red;
92 *fb++ = 0;
93 }
94 }
95}
96
Janne Grunau515a2f72022-02-09 22:16:22 +010097static void draw_unencoded_bitmap(u8 **fbp, uint bpix,
98 enum video_format eformat, uchar *bmap,
Simon Glass646e1692021-11-19 13:23:55 -070099 struct bmp_color_table_entry *palette,
Simon Glassb01c7922016-01-18 19:52:22 -0700100 int cnt)
101{
Simon Glass646e1692021-11-19 13:23:55 -0700102 u8 *fb = *fbp;
103
Simon Glassb01c7922016-01-18 19:52:22 -0700104 while (cnt > 0) {
Janne Grunau515a2f72022-02-09 22:16:22 +0100105 write_pix8(fb, bpix, eformat, palette, bmap++);
Simon Glass646e1692021-11-19 13:23:55 -0700106 fb += bpix / 8;
Simon Glassb01c7922016-01-18 19:52:22 -0700107 cnt--;
108 }
Simon Glass646e1692021-11-19 13:23:55 -0700109 *fbp = fb;
Simon Glassb01c7922016-01-18 19:52:22 -0700110}
111
Janne Grunau515a2f72022-02-09 22:16:22 +0100112static void draw_encoded_bitmap(u8 **fbp, uint bpix, enum video_format eformat,
Simon Glass646e1692021-11-19 13:23:55 -0700113 struct bmp_color_table_entry *palette, u8 *bmap,
114 int cnt)
Simon Glassb01c7922016-01-18 19:52:22 -0700115{
Simon Glass646e1692021-11-19 13:23:55 -0700116 u8 *fb = *fbp;
Simon Glassb01c7922016-01-18 19:52:22 -0700117
118 while (cnt > 0) {
Janne Grunau515a2f72022-02-09 22:16:22 +0100119 write_pix8(fb, bpix, eformat, palette, bmap);
Simon Glass646e1692021-11-19 13:23:55 -0700120 fb += bpix / 8;
Simon Glassb01c7922016-01-18 19:52:22 -0700121 cnt--;
122 }
123 *fbp = fb;
124}
125
126static void video_display_rle8_bitmap(struct udevice *dev,
Simon Glass646e1692021-11-19 13:23:55 -0700127 struct bmp_image *bmp, uint bpix,
128 struct bmp_color_table_entry *palette,
Patrice Chotardca2c6942019-11-20 14:11:16 +0100129 uchar *fb, int x_off, int y_off,
130 ulong width, ulong height)
Simon Glassb01c7922016-01-18 19:52:22 -0700131{
132 struct video_priv *priv = dev_get_uclass_priv(dev);
133 uchar *bmap;
Simon Glassb01c7922016-01-18 19:52:22 -0700134 ulong cnt, runlen;
135 int x, y;
136 int decode = 1;
Simon Glass646e1692021-11-19 13:23:55 -0700137 uint bytes_per_pixel = bpix / 8;
Janne Grunau515a2f72022-02-09 22:16:22 +0100138 enum video_format eformat = priv->format;
Simon Glassb01c7922016-01-18 19:52:22 -0700139
140 debug("%s\n", __func__);
Simon Glassb01c7922016-01-18 19:52:22 -0700141 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
142
143 x = 0;
144 y = height - 1;
145
146 while (decode) {
147 if (bmap[0] == BMP_RLE8_ESCAPE) {
148 switch (bmap[1]) {
149 case BMP_RLE8_EOL:
150 /* end of line */
151 bmap += 2;
152 x = 0;
153 y--;
Simon Glass646e1692021-11-19 13:23:55 -0700154 fb -= width * bytes_per_pixel +
155 priv->line_length;
Simon Glassb01c7922016-01-18 19:52:22 -0700156 break;
157 case BMP_RLE8_EOBMP:
158 /* end of bitmap */
159 decode = 0;
160 break;
161 case BMP_RLE8_DELTA:
162 /* delta run */
163 x += bmap[2];
164 y -= bmap[3];
Simon Glass646e1692021-11-19 13:23:55 -0700165 fb = (uchar *)(priv->fb +
166 (y + y_off - 1) * priv->line_length +
167 (x + x_off) * bytes_per_pixel);
Simon Glassb01c7922016-01-18 19:52:22 -0700168 bmap += 4;
169 break;
170 default:
171 /* unencoded run */
172 runlen = bmap[1];
173 bmap += 2;
174 if (y < height) {
175 if (x < width) {
176 if (x + runlen > width)
177 cnt = width - x;
178 else
179 cnt = runlen;
180 draw_unencoded_bitmap(
Janne Grunau515a2f72022-02-09 22:16:22 +0100181 &fb, bpix, eformat,
Simon Glass646e1692021-11-19 13:23:55 -0700182 bmap, palette, cnt);
Simon Glassb01c7922016-01-18 19:52:22 -0700183 }
184 x += runlen;
185 }
186 bmap += runlen;
187 if (runlen & 1)
188 bmap++;
189 }
190 } else {
191 /* encoded run */
192 if (y < height) {
193 runlen = bmap[0];
194 if (x < width) {
195 /* aggregate the same code */
196 while (bmap[0] == 0xff &&
197 bmap[2] != BMP_RLE8_ESCAPE &&
198 bmap[1] == bmap[3]) {
199 runlen += bmap[2];
200 bmap += 2;
201 }
202 if (x + runlen > width)
203 cnt = width - x;
204 else
205 cnt = runlen;
Janne Grunau515a2f72022-02-09 22:16:22 +0100206 draw_encoded_bitmap(&fb, bpix, eformat,
207 palette, &bmap[1],
208 cnt);
Simon Glassb01c7922016-01-18 19:52:22 -0700209 }
210 x += runlen;
211 }
212 bmap += 2;
213 }
214 }
215}
Simon Glassb01c7922016-01-18 19:52:22 -0700216
Simon Glassb01c7922016-01-18 19:52:22 -0700217/**
218 * video_splash_align_axis() - Align a single coordinate
219 *
220 *- if a coordinate is 0x7fff then the image will be centred in
221 * that direction
222 *- if a coordinate is -ve then it will be offset to the
223 * left/top of the centre by that many pixels
224 *- if a coordinate is positive it will be used unchnaged.
225 *
226 * @axis: Input and output coordinate
227 * @panel_size: Size of panel in pixels for that axis
228 * @picture_size: Size of bitmap in pixels for that axis
229 */
230static void video_splash_align_axis(int *axis, unsigned long panel_size,
231 unsigned long picture_size)
232{
Patrice Chotard1ebf2852019-11-20 14:11:15 +0100233 long panel_picture_delta = panel_size - picture_size;
234 long axis_alignment;
Simon Glassb01c7922016-01-18 19:52:22 -0700235
236 if (*axis == BMP_ALIGN_CENTER)
237 axis_alignment = panel_picture_delta / 2;
238 else if (*axis < 0)
239 axis_alignment = panel_picture_delta + *axis + 1;
240 else
241 return;
242
243 *axis = max(0, (int)axis_alignment);
244}
245
Simon Glasse90322f2022-10-06 08:36:17 -0600246void video_bmp_get_info(void *bmp_image, ulong *widthp, ulong *heightp,
247 uint *bpixp)
248{
249 struct bmp_image *bmp = bmp_image;
250
251 *widthp = get_unaligned_le32(&bmp->header.width);
252 *heightp = get_unaligned_le32(&bmp->header.height);
253 *bpixp = get_unaligned_le16(&bmp->header.bit_count);
254}
255
Simon Glassb01c7922016-01-18 19:52:22 -0700256int video_bmp_display(struct udevice *dev, ulong bmp_image, int x, int y,
257 bool align)
258{
259 struct video_priv *priv = dev_get_uclass_priv(dev);
Simon Glass2b80b4e2016-01-30 15:45:16 -0700260 int i, j;
Simon Glass2b1412c2020-07-02 21:12:27 -0600261 uchar *start, *fb;
Simon Glassb01c7922016-01-18 19:52:22 -0700262 struct bmp_image *bmp = map_sysmem(bmp_image, 0);
263 uchar *bmap;
264 ushort padded_width;
265 unsigned long width, height, byte_width;
266 unsigned long pwidth = priv->xsize;
267 unsigned colours, bpix, bmp_bpix;
Janne Grunau515a2f72022-02-09 22:16:22 +0100268 enum video_format eformat;
Simon Glassb01c7922016-01-18 19:52:22 -0700269 struct bmp_color_table_entry *palette;
270 int hdr_size;
Simon Glass2b1412c2020-07-02 21:12:27 -0600271 int ret;
Simon Glassb01c7922016-01-18 19:52:22 -0700272
273 if (!bmp || !(bmp->header.signature[0] == 'B' &&
274 bmp->header.signature[1] == 'M')) {
275 printf("Error: no valid bmp image at %lx\n", bmp_image);
276
277 return -EINVAL;
278 }
279
Simon Glasse90322f2022-10-06 08:36:17 -0600280 video_bmp_get_info(bmp, &width, &height, &bmp_bpix);
Simon Glassb01c7922016-01-18 19:52:22 -0700281 hdr_size = get_unaligned_le16(&bmp->header.size);
282 debug("hdr_size=%d, bmp_bpix=%d\n", hdr_size, bmp_bpix);
283 palette = (void *)bmp + 14 + hdr_size;
284
285 colours = 1 << bmp_bpix;
286
287 bpix = VNBITS(priv->bpix);
Janne Grunau515a2f72022-02-09 22:16:22 +0100288 eformat = priv->format;
Simon Glassb01c7922016-01-18 19:52:22 -0700289
290 if (bpix != 1 && bpix != 8 && bpix != 16 && bpix != 32) {
291 printf("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
292 bpix, bmp_bpix);
293
294 return -EINVAL;
295 }
296
297 /*
Stefan Roesee2571032019-01-30 08:54:12 +0100298 * We support displaying 8bpp and 24bpp BMPs on 16bpp LCDs
Simon Glassb01c7922016-01-18 19:52:22 -0700299 * and displaying 24bpp BMPs on 32bpp LCDs
Stefan Roesee2571032019-01-30 08:54:12 +0100300 */
Simon Glassb01c7922016-01-18 19:52:22 -0700301 if (bpix != bmp_bpix &&
302 !(bmp_bpix == 8 && bpix == 16) &&
Ye Libab68b22020-06-10 02:52:23 -0700303 !(bmp_bpix == 8 && bpix == 24) &&
304 !(bmp_bpix == 8 && bpix == 32) &&
Stefan Roesee2571032019-01-30 08:54:12 +0100305 !(bmp_bpix == 24 && bpix == 16) &&
Simon Glassb01c7922016-01-18 19:52:22 -0700306 !(bmp_bpix == 24 && bpix == 32)) {
307 printf("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
Simon Glasse90322f2022-10-06 08:36:17 -0600308 bpix, colours);
Simon Glassb01c7922016-01-18 19:52:22 -0700309 return -EPERM;
310 }
311
312 debug("Display-bmp: %d x %d with %d colours, display %d\n",
313 (int)width, (int)height, (int)colours, 1 << bpix);
314
Simon Glassb01c7922016-01-18 19:52:22 -0700315 padded_width = (width & 0x3 ? (width & ~0x3) + 4 : width);
316
317 if (align) {
318 video_splash_align_axis(&x, priv->xsize, width);
319 video_splash_align_axis(&y, priv->ysize, height);
320 }
321
322 if ((x + width) > pwidth)
323 width = pwidth - x;
324 if ((y + height) > priv->ysize)
325 height = priv->ysize - y;
326
327 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
Simon Glass2b1412c2020-07-02 21:12:27 -0600328 start = (uchar *)(priv->fb +
329 (y + height) * priv->line_length + x * bpix / 8);
330
331 /* Move back to the final line to be drawn */
332 fb = start - priv->line_length;
Simon Glassb01c7922016-01-18 19:52:22 -0700333
334 switch (bmp_bpix) {
335 case 1:
Simon Glasscd4fb0f2021-11-19 13:24:00 -0700336 case 8:
Nikhil M Jain86fbee62023-04-20 17:41:08 +0530337 if (CONFIG_IS_ENABLED(VIDEO_BMP_RLE8)) {
Simon Glasscd4fb0f2021-11-19 13:24:00 -0700338 u32 compression = get_unaligned_le32(
339 &bmp->header.compression);
340 debug("compressed %d %d\n", compression, BMP_BI_RLE8);
341 if (compression == BMP_BI_RLE8) {
342 video_display_rle8_bitmap(dev, bmp, bpix, palette, fb,
343 x, y, width, height);
344 break;
345 }
Simon Glassb01c7922016-01-18 19:52:22 -0700346 }
Simon Glasscd4fb0f2021-11-19 13:24:00 -0700347
348 /* Not compressed */
Ye Libab68b22020-06-10 02:52:23 -0700349 byte_width = width * (bpix / 8);
350 if (!byte_width)
Simon Glassb01c7922016-01-18 19:52:22 -0700351 byte_width = width;
Simon Glassb01c7922016-01-18 19:52:22 -0700352
353 for (i = 0; i < height; ++i) {
Stefan Roese29caf932022-09-02 14:10:46 +0200354 schedule();
Simon Glassb01c7922016-01-18 19:52:22 -0700355 for (j = 0; j < width; j++) {
Janne Grunau515a2f72022-02-09 22:16:22 +0100356 write_pix8(fb, bpix, eformat, palette, bmap);
Simon Glass51f92c12021-11-19 13:23:54 -0700357 bmap++;
358 fb += bpix / 8;
Simon Glassb01c7922016-01-18 19:52:22 -0700359 }
360 bmap += (padded_width - width);
361 fb -= byte_width + priv->line_length;
362 }
363 break;
Simon Glassb01c7922016-01-18 19:52:22 -0700364 case 16:
Nikhil M Jain86fbee62023-04-20 17:41:08 +0530365 if (CONFIG_IS_ENABLED(BMP_16BPP)) {
Simon Glasscd4fb0f2021-11-19 13:24:00 -0700366 for (i = 0; i < height; ++i) {
Stefan Roese29caf932022-09-02 14:10:46 +0200367 schedule();
Simon Glasscd4fb0f2021-11-19 13:24:00 -0700368 for (j = 0; j < width; j++) {
Simon Glassf5aa93e2021-11-19 13:23:57 -0700369 *fb++ = *bmap++;
370 *fb++ = *bmap++;
Stefan Roesee2571032019-01-30 08:54:12 +0100371 }
Simon Glasscd4fb0f2021-11-19 13:24:00 -0700372 bmap += (padded_width - width);
373 fb -= width * 2 + priv->line_length;
Simon Glassb01c7922016-01-18 19:52:22 -0700374 }
Simon Glassb01c7922016-01-18 19:52:22 -0700375 }
376 break;
Simon Glasscd4fb0f2021-11-19 13:24:00 -0700377 case 24:
Nikhil M Jain86fbee62023-04-20 17:41:08 +0530378 if (CONFIG_IS_ENABLED(BMP_24BPP)) {
Simon Glasscd4fb0f2021-11-19 13:24:00 -0700379 for (i = 0; i < height; ++i) {
380 for (j = 0; j < width; j++) {
381 if (bpix == 16) {
382 /* 16bit 565RGB format */
383 *(u16 *)fb = ((bmap[2] >> 3)
384 << 11) |
385 ((bmap[1] >> 2) << 5) |
386 (bmap[0] >> 3);
387 bmap += 3;
388 fb += 2;
Janne Grunau515a2f72022-02-09 22:16:22 +0100389 } else if (eformat == VIDEO_X2R10G10B10) {
390 u32 pix;
391
392 pix = *bmap++ << 2U;
393 pix |= *bmap++ << 12U;
394 pix |= *bmap++ << 22U;
395 *fb++ = pix & 0xff;
396 *fb++ = (pix >> 8) & 0xff;
397 *fb++ = (pix >> 16) & 0xff;
398 *fb++ = pix >> 24;
Michal Simekf6de01d2023-05-17 10:42:08 +0200399 } else if (eformat == VIDEO_RGBA8888) {
400 u32 pix;
401
402 pix = *bmap++ << 8U; /* blue */
403 pix |= *bmap++ << 16U; /* green */
404 pix |= *bmap++ << 24U; /* red */
405
406 *fb++ = (pix >> 24) & 0xff;
407 *fb++ = (pix >> 16) & 0xff;
408 *fb++ = (pix >> 8) & 0xff;
409 *fb++ = 0xff;
Simon Glasscd4fb0f2021-11-19 13:24:00 -0700410 } else {
411 *fb++ = *bmap++;
412 *fb++ = *bmap++;
413 *fb++ = *bmap++;
414 *fb++ = 0;
415 }
416 }
417 fb -= priv->line_length + width * (bpix / 8);
418 bmap += (padded_width - width);
419 }
420 }
421 break;
Simon Glassb01c7922016-01-18 19:52:22 -0700422 case 32:
Nikhil M Jain86fbee62023-04-20 17:41:08 +0530423 if (CONFIG_IS_ENABLED(BMP_32BPP)) {
Simon Glasscd4fb0f2021-11-19 13:24:00 -0700424 for (i = 0; i < height; ++i) {
425 for (j = 0; j < width; j++) {
Janne Grunau515a2f72022-02-09 22:16:22 +0100426 if (eformat == VIDEO_X2R10G10B10) {
427 u32 pix;
428
429 pix = *bmap++ << 2U;
430 pix |= *bmap++ << 12U;
431 pix |= *bmap++ << 22U;
432 pix |= (*bmap++ >> 6) << 30U;
433 *fb++ = pix & 0xff;
434 *fb++ = (pix >> 8) & 0xff;
435 *fb++ = (pix >> 16) & 0xff;
436 *fb++ = pix >> 24;
Michal Simekf6de01d2023-05-17 10:42:08 +0200437 } else if (eformat == VIDEO_RGBA8888) {
438 u32 pix;
439
440 pix = *bmap++ << 8U; /* blue */
441 pix |= *bmap++ << 16U; /* green */
442 pix |= *bmap++ << 24U; /* red */
443 bmap++;
444 *fb++ = (pix >> 24) & 0xff;
445 *fb++ = (pix >> 16) & 0xff;
446 *fb++ = (pix >> 8) & 0xff;
447 *fb++ = 0xff; /* opacity */
Janne Grunau515a2f72022-02-09 22:16:22 +0100448 } else {
449 *fb++ = *bmap++;
450 *fb++ = *bmap++;
451 *fb++ = *bmap++;
452 *fb++ = *bmap++;
453 }
Simon Glasscd4fb0f2021-11-19 13:24:00 -0700454 }
455 fb -= priv->line_length + width * (bpix / 8);
Simon Glassb01c7922016-01-18 19:52:22 -0700456 }
Simon Glassb01c7922016-01-18 19:52:22 -0700457 }
458 break;
Simon Glassb01c7922016-01-18 19:52:22 -0700459 default:
460 break;
461 };
462
Simon Glass2b1412c2020-07-02 21:12:27 -0600463 /* Find the position of the top left of the image in the framebuffer */
464 fb = (uchar *)(priv->fb + y * priv->line_length + x * bpix / 8);
465 ret = video_sync_copy(dev, start, fb);
466 if (ret)
467 return log_ret(ret);
468
Michal Simek9de731f2020-12-14 08:47:52 +0100469 return video_sync(dev, false);
Simon Glassb01c7922016-01-18 19:52:22 -0700470}