blob: 4d2d961696aa950a4545514a622f528e02fe127d [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/**
Simon Glass51f92c12021-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 Grunau515a2f72022-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 Glass51f92c12021-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 Grunau515a2f72022-02-09 22:16:22 +010072 } else if (eformat == VIDEO_X2R10G10B10) {
73 *(u32 *)fb = get_bmp_col_x2r10g10b10(cte);
Simon Glass51f92c12021-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 Grunau515a2f72022-02-09 22:16:22 +010083static void draw_unencoded_bitmap(u8 **fbp, uint bpix,
84 enum video_format eformat, uchar *bmap,
Simon Glass646e1692021-11-19 13:23:55 -070085 struct bmp_color_table_entry *palette,
Simon Glassb01c7922016-01-18 19:52:22 -070086 int cnt)
87{
Simon Glass646e1692021-11-19 13:23:55 -070088 u8 *fb = *fbp;
89
Simon Glassb01c7922016-01-18 19:52:22 -070090 while (cnt > 0) {
Janne Grunau515a2f72022-02-09 22:16:22 +010091 write_pix8(fb, bpix, eformat, palette, bmap++);
Simon Glass646e1692021-11-19 13:23:55 -070092 fb += bpix / 8;
Simon Glassb01c7922016-01-18 19:52:22 -070093 cnt--;
94 }
Simon Glass646e1692021-11-19 13:23:55 -070095 *fbp = fb;
Simon Glassb01c7922016-01-18 19:52:22 -070096}
97
Janne Grunau515a2f72022-02-09 22:16:22 +010098static void draw_encoded_bitmap(u8 **fbp, uint bpix, enum video_format eformat,
Simon Glass646e1692021-11-19 13:23:55 -070099 struct bmp_color_table_entry *palette, u8 *bmap,
100 int cnt)
Simon Glassb01c7922016-01-18 19:52:22 -0700101{
Simon Glass646e1692021-11-19 13:23:55 -0700102 u8 *fb = *fbp;
Simon Glassb01c7922016-01-18 19:52:22 -0700103
104 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 }
109 *fbp = fb;
110}
111
112static void video_display_rle8_bitmap(struct udevice *dev,
Simon Glass646e1692021-11-19 13:23:55 -0700113 struct bmp_image *bmp, uint bpix,
114 struct bmp_color_table_entry *palette,
Patrice Chotardca2c6942019-11-20 14:11:16 +0100115 uchar *fb, int x_off, int y_off,
116 ulong width, ulong height)
Simon Glassb01c7922016-01-18 19:52:22 -0700117{
118 struct video_priv *priv = dev_get_uclass_priv(dev);
119 uchar *bmap;
Simon Glassb01c7922016-01-18 19:52:22 -0700120 ulong cnt, runlen;
121 int x, y;
122 int decode = 1;
Simon Glass646e1692021-11-19 13:23:55 -0700123 uint bytes_per_pixel = bpix / 8;
Janne Grunau515a2f72022-02-09 22:16:22 +0100124 enum video_format eformat = priv->format;
Simon Glassb01c7922016-01-18 19:52:22 -0700125
126 debug("%s\n", __func__);
Simon Glassb01c7922016-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 Glass646e1692021-11-19 13:23:55 -0700140 fb -= width * bytes_per_pixel +
141 priv->line_length;
Simon Glassb01c7922016-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 Glass646e1692021-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 Glassb01c7922016-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 Grunau515a2f72022-02-09 22:16:22 +0100167 &fb, bpix, eformat,
Simon Glass646e1692021-11-19 13:23:55 -0700168 bmap, palette, cnt);
Simon Glassb01c7922016-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 Grunau515a2f72022-02-09 22:16:22 +0100192 draw_encoded_bitmap(&fb, bpix, eformat,
193 palette, &bmap[1],
194 cnt);
Simon Glassb01c7922016-01-18 19:52:22 -0700195 }
196 x += runlen;
197 }
198 bmap += 2;
199 }
200 }
201}
Simon Glassb01c7922016-01-18 19:52:22 -0700202
Simon Glassb01c7922016-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 Chotard1ebf2852019-11-20 14:11:15 +0100219 long panel_picture_delta = panel_size - picture_size;
220 long axis_alignment;
Simon Glassb01c7922016-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 Glassb01c7922016-01-18 19:52:22 -0700232int video_bmp_display(struct udevice *dev, ulong bmp_image, int x, int y,
233 bool align)
234{
235 struct video_priv *priv = dev_get_uclass_priv(dev);
Simon Glass2b80b4e2016-01-30 15:45:16 -0700236 int i, j;
Simon Glass2b1412c2020-07-02 21:12:27 -0600237 uchar *start, *fb;
Simon Glassb01c7922016-01-18 19:52:22 -0700238 struct bmp_image *bmp = map_sysmem(bmp_image, 0);
239 uchar *bmap;
240 ushort padded_width;
241 unsigned long width, height, byte_width;
242 unsigned long pwidth = priv->xsize;
243 unsigned colours, bpix, bmp_bpix;
Janne Grunau515a2f72022-02-09 22:16:22 +0100244 enum video_format eformat;
Simon Glassb01c7922016-01-18 19:52:22 -0700245 struct bmp_color_table_entry *palette;
246 int hdr_size;
Simon Glass2b1412c2020-07-02 21:12:27 -0600247 int ret;
Simon Glassb01c7922016-01-18 19:52:22 -0700248
249 if (!bmp || !(bmp->header.signature[0] == 'B' &&
250 bmp->header.signature[1] == 'M')) {
251 printf("Error: no valid bmp image at %lx\n", bmp_image);
252
253 return -EINVAL;
254 }
255
256 width = get_unaligned_le32(&bmp->header.width);
257 height = get_unaligned_le32(&bmp->header.height);
258 bmp_bpix = get_unaligned_le16(&bmp->header.bit_count);
259 hdr_size = get_unaligned_le16(&bmp->header.size);
260 debug("hdr_size=%d, bmp_bpix=%d\n", hdr_size, bmp_bpix);
261 palette = (void *)bmp + 14 + hdr_size;
262
263 colours = 1 << bmp_bpix;
264
265 bpix = VNBITS(priv->bpix);
Janne Grunau515a2f72022-02-09 22:16:22 +0100266 eformat = priv->format;
Simon Glassb01c7922016-01-18 19:52:22 -0700267
268 if (bpix != 1 && bpix != 8 && bpix != 16 && bpix != 32) {
269 printf("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
270 bpix, bmp_bpix);
271
272 return -EINVAL;
273 }
274
275 /*
Stefan Roesee2571032019-01-30 08:54:12 +0100276 * We support displaying 8bpp and 24bpp BMPs on 16bpp LCDs
Simon Glassb01c7922016-01-18 19:52:22 -0700277 * and displaying 24bpp BMPs on 32bpp LCDs
Stefan Roesee2571032019-01-30 08:54:12 +0100278 */
Simon Glassb01c7922016-01-18 19:52:22 -0700279 if (bpix != bmp_bpix &&
280 !(bmp_bpix == 8 && bpix == 16) &&
Ye Libab68b22020-06-10 02:52:23 -0700281 !(bmp_bpix == 8 && bpix == 24) &&
282 !(bmp_bpix == 8 && bpix == 32) &&
Stefan Roesee2571032019-01-30 08:54:12 +0100283 !(bmp_bpix == 24 && bpix == 16) &&
Simon Glassb01c7922016-01-18 19:52:22 -0700284 !(bmp_bpix == 24 && bpix == 32)) {
285 printf("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
286 bpix, get_unaligned_le16(&bmp->header.bit_count));
287 return -EPERM;
288 }
289
290 debug("Display-bmp: %d x %d with %d colours, display %d\n",
291 (int)width, (int)height, (int)colours, 1 << bpix);
292
Simon Glassb01c7922016-01-18 19:52:22 -0700293 padded_width = (width & 0x3 ? (width & ~0x3) + 4 : width);
294
295 if (align) {
296 video_splash_align_axis(&x, priv->xsize, width);
297 video_splash_align_axis(&y, priv->ysize, height);
298 }
299
300 if ((x + width) > pwidth)
301 width = pwidth - x;
302 if ((y + height) > priv->ysize)
303 height = priv->ysize - y;
304
305 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
Simon Glass2b1412c2020-07-02 21:12:27 -0600306 start = (uchar *)(priv->fb +
307 (y + height) * priv->line_length + x * bpix / 8);
308
309 /* Move back to the final line to be drawn */
310 fb = start - priv->line_length;
Simon Glassb01c7922016-01-18 19:52:22 -0700311
312 switch (bmp_bpix) {
313 case 1:
Simon Glasscd4fb0f2021-11-19 13:24:00 -0700314 case 8:
315 if (IS_ENABLED(CONFIG_VIDEO_BMP_RLE8)) {
316 u32 compression = get_unaligned_le32(
317 &bmp->header.compression);
318 debug("compressed %d %d\n", compression, BMP_BI_RLE8);
319 if (compression == BMP_BI_RLE8) {
320 video_display_rle8_bitmap(dev, bmp, bpix, palette, fb,
321 x, y, width, height);
322 break;
323 }
Simon Glassb01c7922016-01-18 19:52:22 -0700324 }
Simon Glasscd4fb0f2021-11-19 13:24:00 -0700325
326 /* Not compressed */
Ye Libab68b22020-06-10 02:52:23 -0700327 byte_width = width * (bpix / 8);
328 if (!byte_width)
Simon Glassb01c7922016-01-18 19:52:22 -0700329 byte_width = width;
Simon Glassb01c7922016-01-18 19:52:22 -0700330
331 for (i = 0; i < height; ++i) {
332 WATCHDOG_RESET();
333 for (j = 0; j < width; j++) {
Janne Grunau515a2f72022-02-09 22:16:22 +0100334 write_pix8(fb, bpix, eformat, palette, bmap);
Simon Glass51f92c12021-11-19 13:23:54 -0700335 bmap++;
336 fb += bpix / 8;
Simon Glassb01c7922016-01-18 19:52:22 -0700337 }
338 bmap += (padded_width - width);
339 fb -= byte_width + priv->line_length;
340 }
341 break;
Simon Glassb01c7922016-01-18 19:52:22 -0700342 case 16:
Simon Glasscd4fb0f2021-11-19 13:24:00 -0700343 if (IS_ENABLED(CONFIG_BMP_16BPP)) {
344 for (i = 0; i < height; ++i) {
345 WATCHDOG_RESET();
346 for (j = 0; j < width; j++) {
Simon Glassf5aa93e2021-11-19 13:23:57 -0700347 *fb++ = *bmap++;
348 *fb++ = *bmap++;
Stefan Roesee2571032019-01-30 08:54:12 +0100349 }
Simon Glasscd4fb0f2021-11-19 13:24:00 -0700350 bmap += (padded_width - width);
351 fb -= width * 2 + priv->line_length;
Simon Glassb01c7922016-01-18 19:52:22 -0700352 }
Simon Glassb01c7922016-01-18 19:52:22 -0700353 }
354 break;
Simon Glasscd4fb0f2021-11-19 13:24:00 -0700355 case 24:
356 if (IS_ENABLED(CONFIG_BMP_24BPP)) {
357 for (i = 0; i < height; ++i) {
358 for (j = 0; j < width; j++) {
359 if (bpix == 16) {
360 /* 16bit 565RGB format */
361 *(u16 *)fb = ((bmap[2] >> 3)
362 << 11) |
363 ((bmap[1] >> 2) << 5) |
364 (bmap[0] >> 3);
365 bmap += 3;
366 fb += 2;
Janne Grunau515a2f72022-02-09 22:16:22 +0100367 } else if (eformat == VIDEO_X2R10G10B10) {
368 u32 pix;
369
370 pix = *bmap++ << 2U;
371 pix |= *bmap++ << 12U;
372 pix |= *bmap++ << 22U;
373 *fb++ = pix & 0xff;
374 *fb++ = (pix >> 8) & 0xff;
375 *fb++ = (pix >> 16) & 0xff;
376 *fb++ = pix >> 24;
Simon Glasscd4fb0f2021-11-19 13:24:00 -0700377 } else {
378 *fb++ = *bmap++;
379 *fb++ = *bmap++;
380 *fb++ = *bmap++;
381 *fb++ = 0;
382 }
383 }
384 fb -= priv->line_length + width * (bpix / 8);
385 bmap += (padded_width - width);
386 }
387 }
388 break;
Simon Glassb01c7922016-01-18 19:52:22 -0700389 case 32:
Simon Glasscd4fb0f2021-11-19 13:24:00 -0700390 if (IS_ENABLED(CONFIG_BMP_32BPP)) {
391 for (i = 0; i < height; ++i) {
392 for (j = 0; j < width; j++) {
Janne Grunau515a2f72022-02-09 22:16:22 +0100393 if (eformat == VIDEO_X2R10G10B10) {
394 u32 pix;
395
396 pix = *bmap++ << 2U;
397 pix |= *bmap++ << 12U;
398 pix |= *bmap++ << 22U;
399 pix |= (*bmap++ >> 6) << 30U;
400 *fb++ = pix & 0xff;
401 *fb++ = (pix >> 8) & 0xff;
402 *fb++ = (pix >> 16) & 0xff;
403 *fb++ = pix >> 24;
404 } else {
405 *fb++ = *bmap++;
406 *fb++ = *bmap++;
407 *fb++ = *bmap++;
408 *fb++ = *bmap++;
409 }
Simon Glasscd4fb0f2021-11-19 13:24:00 -0700410 }
411 fb -= priv->line_length + width * (bpix / 8);
Simon Glassb01c7922016-01-18 19:52:22 -0700412 }
Simon Glassb01c7922016-01-18 19:52:22 -0700413 }
414 break;
Simon Glassb01c7922016-01-18 19:52:22 -0700415 default:
416 break;
417 };
418
Simon Glass2b1412c2020-07-02 21:12:27 -0600419 /* Find the position of the top left of the image in the framebuffer */
420 fb = (uchar *)(priv->fb + y * priv->line_length + x * bpix / 8);
421 ret = video_sync_copy(dev, start, fb);
422 if (ret)
423 return log_ret(ret);
424
Michal Simek9de731f2020-12-14 08:47:52 +0100425 return video_sync(dev, false);
Simon Glassb01c7922016-01-18 19:52:22 -0700426}