blob: 6188a13e44e472d0c8149e0dcbd3a5ffe9a0ce30 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass68b26272016-01-18 19:52:22 -07002/*
3 * Copyright (c) 2015 Google, Inc
Simon Glass68b26272016-01-18 19:52:22 -07004 */
5
6#include <common.h>
7#include <bmp_layout.h>
8#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Simon Glass68b26272016-01-18 19:52:22 -070010#include <mapmem.h>
Anatolij Gustschin410f0962018-12-01 15:30:08 +010011#include <splash.h>
Simon Glass68b26272016-01-18 19:52:22 -070012#include <video.h>
13#include <watchdog.h>
14#include <asm/unaligned.h>
15
Simon Glass68b26272016-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 Glass998e16a2021-11-19 13:23:54 -070021/**
22 * get_bmp_col_16bpp() - Convert a colour-table entry into a 16bpp pixel value
23 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010024 * Return: value to write to the 16bpp frame buffer for this palette entry
Simon Glass998e16a2021-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 Grunau8296d142022-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/**
Simon Glass998e16a2021-11-19 13:23:54 -070046 * write_pix8() - Write a pixel from a BMP image into the framebuffer
47 *
48 * This handles frame buffers with 8, 16, 24 or 32 bits per pixel
49 *
50 * @fb: Place in frame buffer to update
51 * @bpix: Frame buffer bits-per-pixel, which controls how many bytes are written
52 * @palette: BMP palette table
53 * @bmap: Pointer to BMP bitmap position to write. This contains a single byte
54 * which is either written directly (bpix == 8) or used to look up the
55 * palette to get a colour to write
56 */
Janne Grunau8296d142022-02-09 22:16:22 +010057static void write_pix8(u8 *fb, uint bpix, enum video_format eformat,
58 struct bmp_color_table_entry *palette, u8 *bmap)
Simon Glass998e16a2021-11-19 13:23:54 -070059{
60 if (bpix == 8) {
61 *fb++ = *bmap;
62 } else if (bpix == 16) {
63 *(u16 *)fb = get_bmp_col_16bpp(palette[*bmap]);
64 } else {
65 /* Only support big endian */
66 struct bmp_color_table_entry *cte = &palette[*bmap];
67
68 if (bpix == 24) {
69 *fb++ = cte->red;
70 *fb++ = cte->green;
71 *fb++ = cte->blue;
Janne Grunau8296d142022-02-09 22:16:22 +010072 } else if (eformat == VIDEO_X2R10G10B10) {
73 *(u32 *)fb = get_bmp_col_x2r10g10b10(cte);
Simon Glass998e16a2021-11-19 13:23:54 -070074 } else {
75 *fb++ = cte->blue;
76 *fb++ = cte->green;
77 *fb++ = cte->red;
78 *fb++ = 0;
79 }
80 }
81}
82
Janne Grunau8296d142022-02-09 22:16:22 +010083static void draw_unencoded_bitmap(u8 **fbp, uint bpix,
84 enum video_format eformat, uchar *bmap,
Simon Glass490bb992021-11-19 13:23:55 -070085 struct bmp_color_table_entry *palette,
Simon Glass68b26272016-01-18 19:52:22 -070086 int cnt)
87{
Simon Glass490bb992021-11-19 13:23:55 -070088 u8 *fb = *fbp;
89
Simon Glass68b26272016-01-18 19:52:22 -070090 while (cnt > 0) {
Janne Grunau8296d142022-02-09 22:16:22 +010091 write_pix8(fb, bpix, eformat, palette, bmap++);
Simon Glass490bb992021-11-19 13:23:55 -070092 fb += bpix / 8;
Simon Glass68b26272016-01-18 19:52:22 -070093 cnt--;
94 }
Simon Glass490bb992021-11-19 13:23:55 -070095 *fbp = fb;
Simon Glass68b26272016-01-18 19:52:22 -070096}
97
Janne Grunau8296d142022-02-09 22:16:22 +010098static void draw_encoded_bitmap(u8 **fbp, uint bpix, enum video_format eformat,
Simon Glass490bb992021-11-19 13:23:55 -070099 struct bmp_color_table_entry *palette, u8 *bmap,
100 int cnt)
Simon Glass68b26272016-01-18 19:52:22 -0700101{
Simon Glass490bb992021-11-19 13:23:55 -0700102 u8 *fb = *fbp;
Simon Glass68b26272016-01-18 19:52:22 -0700103
104 while (cnt > 0) {
Janne Grunau8296d142022-02-09 22:16:22 +0100105 write_pix8(fb, bpix, eformat, palette, bmap);
Simon Glass490bb992021-11-19 13:23:55 -0700106 fb += bpix / 8;
Simon Glass68b26272016-01-18 19:52:22 -0700107 cnt--;
108 }
109 *fbp = fb;
110}
111
112static void video_display_rle8_bitmap(struct udevice *dev,
Simon Glass490bb992021-11-19 13:23:55 -0700113 struct bmp_image *bmp, uint bpix,
114 struct bmp_color_table_entry *palette,
Patrice Chotardcb30f7d2019-11-20 14:11:16 +0100115 uchar *fb, int x_off, int y_off,
116 ulong width, ulong height)
Simon Glass68b26272016-01-18 19:52:22 -0700117{
118 struct video_priv *priv = dev_get_uclass_priv(dev);
119 uchar *bmap;
Simon Glass68b26272016-01-18 19:52:22 -0700120 ulong cnt, runlen;
121 int x, y;
122 int decode = 1;
Simon Glass490bb992021-11-19 13:23:55 -0700123 uint bytes_per_pixel = bpix / 8;
Janne Grunau8296d142022-02-09 22:16:22 +0100124 enum video_format eformat = priv->format;
Simon Glass68b26272016-01-18 19:52:22 -0700125
126 debug("%s\n", __func__);
Simon Glass68b26272016-01-18 19:52:22 -0700127 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
128
129 x = 0;
130 y = height - 1;
131
132 while (decode) {
133 if (bmap[0] == BMP_RLE8_ESCAPE) {
134 switch (bmap[1]) {
135 case BMP_RLE8_EOL:
136 /* end of line */
137 bmap += 2;
138 x = 0;
139 y--;
Simon Glass490bb992021-11-19 13:23:55 -0700140 fb -= width * bytes_per_pixel +
141 priv->line_length;
Simon Glass68b26272016-01-18 19:52:22 -0700142 break;
143 case BMP_RLE8_EOBMP:
144 /* end of bitmap */
145 decode = 0;
146 break;
147 case BMP_RLE8_DELTA:
148 /* delta run */
149 x += bmap[2];
150 y -= bmap[3];
Simon Glass490bb992021-11-19 13:23:55 -0700151 fb = (uchar *)(priv->fb +
152 (y + y_off - 1) * priv->line_length +
153 (x + x_off) * bytes_per_pixel);
Simon Glass68b26272016-01-18 19:52:22 -0700154 bmap += 4;
155 break;
156 default:
157 /* unencoded run */
158 runlen = bmap[1];
159 bmap += 2;
160 if (y < height) {
161 if (x < width) {
162 if (x + runlen > width)
163 cnt = width - x;
164 else
165 cnt = runlen;
166 draw_unencoded_bitmap(
Janne Grunau8296d142022-02-09 22:16:22 +0100167 &fb, bpix, eformat,
Simon Glass490bb992021-11-19 13:23:55 -0700168 bmap, palette, cnt);
Simon Glass68b26272016-01-18 19:52:22 -0700169 }
170 x += runlen;
171 }
172 bmap += runlen;
173 if (runlen & 1)
174 bmap++;
175 }
176 } else {
177 /* encoded run */
178 if (y < height) {
179 runlen = bmap[0];
180 if (x < width) {
181 /* aggregate the same code */
182 while (bmap[0] == 0xff &&
183 bmap[2] != BMP_RLE8_ESCAPE &&
184 bmap[1] == bmap[3]) {
185 runlen += bmap[2];
186 bmap += 2;
187 }
188 if (x + runlen > width)
189 cnt = width - x;
190 else
191 cnt = runlen;
Janne Grunau8296d142022-02-09 22:16:22 +0100192 draw_encoded_bitmap(&fb, bpix, eformat,
193 palette, &bmap[1],
194 cnt);
Simon Glass68b26272016-01-18 19:52:22 -0700195 }
196 x += runlen;
197 }
198 bmap += 2;
199 }
200 }
201}
Simon Glass68b26272016-01-18 19:52:22 -0700202
Simon Glass68b26272016-01-18 19:52:22 -0700203/**
204 * video_splash_align_axis() - Align a single coordinate
205 *
206 *- if a coordinate is 0x7fff then the image will be centred in
207 * that direction
208 *- if a coordinate is -ve then it will be offset to the
209 * left/top of the centre by that many pixels
210 *- if a coordinate is positive it will be used unchnaged.
211 *
212 * @axis: Input and output coordinate
213 * @panel_size: Size of panel in pixels for that axis
214 * @picture_size: Size of bitmap in pixels for that axis
215 */
216static void video_splash_align_axis(int *axis, unsigned long panel_size,
217 unsigned long picture_size)
218{
Patrice Chotard066eace2019-11-20 14:11:15 +0100219 long panel_picture_delta = panel_size - picture_size;
220 long axis_alignment;
Simon Glass68b26272016-01-18 19:52:22 -0700221
222 if (*axis == BMP_ALIGN_CENTER)
223 axis_alignment = panel_picture_delta / 2;
224 else if (*axis < 0)
225 axis_alignment = panel_picture_delta + *axis + 1;
226 else
227 return;
228
229 *axis = max(0, (int)axis_alignment);
230}
231
Simon Glass7e148042022-10-06 08:36:17 -0600232void video_bmp_get_info(void *bmp_image, ulong *widthp, ulong *heightp,
233 uint *bpixp)
234{
235 struct bmp_image *bmp = bmp_image;
236
237 *widthp = get_unaligned_le32(&bmp->header.width);
238 *heightp = get_unaligned_le32(&bmp->header.height);
239 *bpixp = get_unaligned_le16(&bmp->header.bit_count);
240}
241
Simon Glass68b26272016-01-18 19:52:22 -0700242int video_bmp_display(struct udevice *dev, ulong bmp_image, int x, int y,
243 bool align)
244{
245 struct video_priv *priv = dev_get_uclass_priv(dev);
Simon Glass1f475732016-01-30 15:45:16 -0700246 int i, j;
Simon Glass4761c8c2020-07-02 21:12:27 -0600247 uchar *start, *fb;
Simon Glass68b26272016-01-18 19:52:22 -0700248 struct bmp_image *bmp = map_sysmem(bmp_image, 0);
249 uchar *bmap;
250 ushort padded_width;
251 unsigned long width, height, byte_width;
252 unsigned long pwidth = priv->xsize;
253 unsigned colours, bpix, bmp_bpix;
Janne Grunau8296d142022-02-09 22:16:22 +0100254 enum video_format eformat;
Simon Glass68b26272016-01-18 19:52:22 -0700255 struct bmp_color_table_entry *palette;
256 int hdr_size;
Simon Glass4761c8c2020-07-02 21:12:27 -0600257 int ret;
Simon Glass68b26272016-01-18 19:52:22 -0700258
259 if (!bmp || !(bmp->header.signature[0] == 'B' &&
260 bmp->header.signature[1] == 'M')) {
261 printf("Error: no valid bmp image at %lx\n", bmp_image);
262
263 return -EINVAL;
264 }
265
Simon Glass7e148042022-10-06 08:36:17 -0600266 video_bmp_get_info(bmp, &width, &height, &bmp_bpix);
Simon Glass68b26272016-01-18 19:52:22 -0700267 hdr_size = get_unaligned_le16(&bmp->header.size);
268 debug("hdr_size=%d, bmp_bpix=%d\n", hdr_size, bmp_bpix);
269 palette = (void *)bmp + 14 + hdr_size;
270
271 colours = 1 << bmp_bpix;
272
273 bpix = VNBITS(priv->bpix);
Janne Grunau8296d142022-02-09 22:16:22 +0100274 eformat = priv->format;
Simon Glass68b26272016-01-18 19:52:22 -0700275
276 if (bpix != 1 && bpix != 8 && bpix != 16 && bpix != 32) {
277 printf("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
278 bpix, bmp_bpix);
279
280 return -EINVAL;
281 }
282
283 /*
Stefan Roese7ee599e2019-01-30 08:54:12 +0100284 * We support displaying 8bpp and 24bpp BMPs on 16bpp LCDs
Simon Glass68b26272016-01-18 19:52:22 -0700285 * and displaying 24bpp BMPs on 32bpp LCDs
Stefan Roese7ee599e2019-01-30 08:54:12 +0100286 */
Simon Glass68b26272016-01-18 19:52:22 -0700287 if (bpix != bmp_bpix &&
288 !(bmp_bpix == 8 && bpix == 16) &&
Ye Lib5c864d2020-06-10 02:52:23 -0700289 !(bmp_bpix == 8 && bpix == 24) &&
290 !(bmp_bpix == 8 && bpix == 32) &&
Stefan Roese7ee599e2019-01-30 08:54:12 +0100291 !(bmp_bpix == 24 && bpix == 16) &&
Simon Glass68b26272016-01-18 19:52:22 -0700292 !(bmp_bpix == 24 && bpix == 32)) {
293 printf("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
Simon Glass7e148042022-10-06 08:36:17 -0600294 bpix, colours);
Simon Glass68b26272016-01-18 19:52:22 -0700295 return -EPERM;
296 }
297
298 debug("Display-bmp: %d x %d with %d colours, display %d\n",
299 (int)width, (int)height, (int)colours, 1 << bpix);
300
Simon Glass68b26272016-01-18 19:52:22 -0700301 padded_width = (width & 0x3 ? (width & ~0x3) + 4 : width);
302
303 if (align) {
304 video_splash_align_axis(&x, priv->xsize, width);
305 video_splash_align_axis(&y, priv->ysize, height);
306 }
307
308 if ((x + width) > pwidth)
309 width = pwidth - x;
310 if ((y + height) > priv->ysize)
311 height = priv->ysize - y;
312
313 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
Simon Glass4761c8c2020-07-02 21:12:27 -0600314 start = (uchar *)(priv->fb +
315 (y + height) * priv->line_length + x * bpix / 8);
316
317 /* Move back to the final line to be drawn */
318 fb = start - priv->line_length;
Simon Glass68b26272016-01-18 19:52:22 -0700319
320 switch (bmp_bpix) {
321 case 1:
Simon Glass61494702021-11-19 13:24:00 -0700322 case 8:
323 if (IS_ENABLED(CONFIG_VIDEO_BMP_RLE8)) {
324 u32 compression = get_unaligned_le32(
325 &bmp->header.compression);
326 debug("compressed %d %d\n", compression, BMP_BI_RLE8);
327 if (compression == BMP_BI_RLE8) {
328 video_display_rle8_bitmap(dev, bmp, bpix, palette, fb,
329 x, y, width, height);
330 break;
331 }
Simon Glass68b26272016-01-18 19:52:22 -0700332 }
Simon Glass61494702021-11-19 13:24:00 -0700333
334 /* Not compressed */
Ye Lib5c864d2020-06-10 02:52:23 -0700335 byte_width = width * (bpix / 8);
336 if (!byte_width)
Simon Glass68b26272016-01-18 19:52:22 -0700337 byte_width = width;
Simon Glass68b26272016-01-18 19:52:22 -0700338
339 for (i = 0; i < height; ++i) {
Stefan Roese80877fa2022-09-02 14:10:46 +0200340 schedule();
Simon Glass68b26272016-01-18 19:52:22 -0700341 for (j = 0; j < width; j++) {
Janne Grunau8296d142022-02-09 22:16:22 +0100342 write_pix8(fb, bpix, eformat, palette, bmap);
Simon Glass998e16a2021-11-19 13:23:54 -0700343 bmap++;
344 fb += bpix / 8;
Simon Glass68b26272016-01-18 19:52:22 -0700345 }
346 bmap += (padded_width - width);
347 fb -= byte_width + priv->line_length;
348 }
349 break;
Simon Glass68b26272016-01-18 19:52:22 -0700350 case 16:
Simon Glass61494702021-11-19 13:24:00 -0700351 if (IS_ENABLED(CONFIG_BMP_16BPP)) {
352 for (i = 0; i < height; ++i) {
Stefan Roese80877fa2022-09-02 14:10:46 +0200353 schedule();
Simon Glass61494702021-11-19 13:24:00 -0700354 for (j = 0; j < width; j++) {
355 *fb++ = *bmap++;
356 *fb++ = *bmap++;
357 }
358 bmap += (padded_width - width);
359 fb -= width * 2 + priv->line_length;
Simon Glassaa662a62021-11-19 13:23:53 -0700360 }
Simon Glass68b26272016-01-18 19:52:22 -0700361 }
362 break;
Simon Glass68b26272016-01-18 19:52:22 -0700363 case 24:
Simon Glass61494702021-11-19 13:24:00 -0700364 if (IS_ENABLED(CONFIG_BMP_24BPP)) {
365 for (i = 0; i < height; ++i) {
366 for (j = 0; j < width; j++) {
367 if (bpix == 16) {
368 /* 16bit 565RGB format */
369 *(u16 *)fb = ((bmap[2] >> 3)
370 << 11) |
371 ((bmap[1] >> 2) << 5) |
372 (bmap[0] >> 3);
373 bmap += 3;
374 fb += 2;
Janne Grunau8296d142022-02-09 22:16:22 +0100375 } else if (eformat == VIDEO_X2R10G10B10) {
376 u32 pix;
377
378 pix = *bmap++ << 2U;
379 pix |= *bmap++ << 12U;
380 pix |= *bmap++ << 22U;
381 *fb++ = pix & 0xff;
382 *fb++ = (pix >> 8) & 0xff;
383 *fb++ = (pix >> 16) & 0xff;
384 *fb++ = pix >> 24;
Simon Glass61494702021-11-19 13:24:00 -0700385 } else {
386 *fb++ = *bmap++;
387 *fb++ = *bmap++;
388 *fb++ = *bmap++;
389 *fb++ = 0;
390 }
Stefan Roese7ee599e2019-01-30 08:54:12 +0100391 }
Simon Glass61494702021-11-19 13:24:00 -0700392 fb -= priv->line_length + width * (bpix / 8);
393 bmap += (padded_width - width);
Simon Glass68b26272016-01-18 19:52:22 -0700394 }
Simon Glass68b26272016-01-18 19:52:22 -0700395 }
396 break;
Simon Glass68b26272016-01-18 19:52:22 -0700397 case 32:
Simon Glass61494702021-11-19 13:24:00 -0700398 if (IS_ENABLED(CONFIG_BMP_32BPP)) {
399 for (i = 0; i < height; ++i) {
400 for (j = 0; j < width; j++) {
Janne Grunau8296d142022-02-09 22:16:22 +0100401 if (eformat == VIDEO_X2R10G10B10) {
402 u32 pix;
403
404 pix = *bmap++ << 2U;
405 pix |= *bmap++ << 12U;
406 pix |= *bmap++ << 22U;
407 pix |= (*bmap++ >> 6) << 30U;
408 *fb++ = pix & 0xff;
409 *fb++ = (pix >> 8) & 0xff;
410 *fb++ = (pix >> 16) & 0xff;
411 *fb++ = pix >> 24;
412 } else {
413 *fb++ = *bmap++;
414 *fb++ = *bmap++;
415 *fb++ = *bmap++;
416 *fb++ = *bmap++;
417 }
Simon Glass61494702021-11-19 13:24:00 -0700418 }
419 fb -= priv->line_length + width * (bpix / 8);
Simon Glass68b26272016-01-18 19:52:22 -0700420 }
Simon Glass68b26272016-01-18 19:52:22 -0700421 }
422 break;
Simon Glass68b26272016-01-18 19:52:22 -0700423 default:
424 break;
425 };
426
Simon Glass4761c8c2020-07-02 21:12:27 -0600427 /* Find the position of the top left of the image in the framebuffer */
428 fb = (uchar *)(priv->fb + y * priv->line_length + x * bpix / 8);
429 ret = video_sync_copy(dev, start, fb);
430 if (ret)
431 return log_ret(ret);
432
Michal Simek632e3d42020-12-14 08:47:52 +0100433 return video_sync(dev, false);
Simon Glass68b26272016-01-18 19:52:22 -0700434}