1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
7 #include <bmp_layout.h>
14 #include <asm/unaligned.h>
16 #define BMP_RLE8_ESCAPE 0
17 #define BMP_RLE8_EOL 0
18 #define BMP_RLE8_EOBMP 1
19 #define BMP_RLE8_DELTA 2
22 * get_bmp_col_16bpp() - Convert a colour-table entry into a 16bpp pixel value
24 * Return: value to write to the 16bpp frame buffer for this palette entry
26 static uint get_bmp_col_16bpp(struct bmp_color_table_entry cte)
28 return ((cte.red << 8) & 0xf800) |
29 ((cte.green << 3) & 0x07e0) |
30 ((cte.blue >> 3) & 0x001f);
34 * get_bmp_col_x2r10g10b10() - Convert a colour-table entry into a x2r10g10b10 pixel value
36 * Return: value to write to the x2r10g10b10 frame buffer for this palette entry
38 static u32 get_bmp_col_x2r10g10b10(struct bmp_color_table_entry *cte)
40 return ((cte->red << 22U) |
46 * write_pix8() - Write a pixel from a BMP image into the framebuffer
48 * This handles frame buffers with 8, 16, 24 or 32 bits per pixel
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
57 static void write_pix8(u8 *fb, uint bpix, enum video_format eformat,
58 struct bmp_color_table_entry *palette, u8 *bmap)
62 } else if (bpix == 16) {
63 *(u16 *)fb = get_bmp_col_16bpp(palette[*bmap]);
65 /* Only support big endian */
66 struct bmp_color_table_entry *cte = &palette[*bmap];
72 } else if (eformat == VIDEO_X2R10G10B10) {
73 *(u32 *)fb = get_bmp_col_x2r10g10b10(cte);
83 static void draw_unencoded_bitmap(u8 **fbp, uint bpix,
84 enum video_format eformat, uchar *bmap,
85 struct bmp_color_table_entry *palette,
91 write_pix8(fb, bpix, eformat, palette, bmap++);
98 static void draw_encoded_bitmap(u8 **fbp, uint bpix, enum video_format eformat,
99 struct bmp_color_table_entry *palette, u8 *bmap,
105 write_pix8(fb, bpix, eformat, palette, bmap);
112 static void video_display_rle8_bitmap(struct udevice *dev,
113 struct bmp_image *bmp, uint bpix,
114 struct bmp_color_table_entry *palette,
115 uchar *fb, int x_off, int y_off,
116 ulong width, ulong height)
118 struct video_priv *priv = dev_get_uclass_priv(dev);
123 uint bytes_per_pixel = bpix / 8;
124 enum video_format eformat = priv->format;
126 debug("%s\n", __func__);
127 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
133 if (bmap[0] == BMP_RLE8_ESCAPE) {
140 fb -= width * bytes_per_pixel +
151 fb = (uchar *)(priv->fb +
152 (y + y_off - 1) * priv->line_length +
153 (x + x_off) * bytes_per_pixel);
162 if (x + runlen > width)
166 draw_unencoded_bitmap(
181 /* aggregate the same code */
182 while (bmap[0] == 0xff &&
183 bmap[2] != BMP_RLE8_ESCAPE &&
184 bmap[1] == bmap[3]) {
188 if (x + runlen > width)
192 draw_encoded_bitmap(&fb, bpix, eformat,
204 * video_splash_align_axis() - Align a single coordinate
206 *- if a coordinate is 0x7fff then the image will be centred in
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.
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
216 static void video_splash_align_axis(int *axis, unsigned long panel_size,
217 unsigned long picture_size)
219 long panel_picture_delta = panel_size - picture_size;
222 if (*axis == BMP_ALIGN_CENTER)
223 axis_alignment = panel_picture_delta / 2;
225 axis_alignment = panel_picture_delta + *axis + 1;
229 *axis = max(0, (int)axis_alignment);
232 void video_bmp_get_info(void *bmp_image, ulong *widthp, ulong *heightp,
235 struct bmp_image *bmp = bmp_image;
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);
242 int video_bmp_display(struct udevice *dev, ulong bmp_image, int x, int y,
245 struct video_priv *priv = dev_get_uclass_priv(dev);
248 struct bmp_image *bmp = map_sysmem(bmp_image, 0);
251 unsigned long width, height, byte_width;
252 unsigned long pwidth = priv->xsize;
253 unsigned colours, bpix, bmp_bpix;
254 enum video_format eformat;
255 struct bmp_color_table_entry *palette;
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);
266 video_bmp_get_info(bmp, &width, &height, &bmp_bpix);
267 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;
271 colours = 1 << bmp_bpix;
273 bpix = VNBITS(priv->bpix);
274 eformat = priv->format;
276 if (bpix != 1 && bpix != 8 && bpix != 16 && bpix != 32) {
277 printf("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
284 * We support displaying 8bpp and 24bpp BMPs on 16bpp LCDs
285 * and displaying 24bpp BMPs on 32bpp LCDs
287 if (bpix != bmp_bpix &&
288 !(bmp_bpix == 8 && bpix == 16) &&
289 !(bmp_bpix == 8 && bpix == 24) &&
290 !(bmp_bpix == 8 && bpix == 32) &&
291 !(bmp_bpix == 24 && bpix == 16) &&
292 !(bmp_bpix == 24 && bpix == 32)) {
293 printf("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
298 debug("Display-bmp: %d x %d with %d colours, display %d\n",
299 (int)width, (int)height, (int)colours, 1 << bpix);
301 padded_width = (width & 0x3 ? (width & ~0x3) + 4 : width);
304 video_splash_align_axis(&x, priv->xsize, width);
305 video_splash_align_axis(&y, priv->ysize, height);
308 if ((x + width) > pwidth)
310 if ((y + height) > priv->ysize)
311 height = priv->ysize - y;
313 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
314 start = (uchar *)(priv->fb +
315 (y + height) * priv->line_length + x * bpix / 8);
317 /* Move back to the final line to be drawn */
318 fb = start - priv->line_length;
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);
335 byte_width = width * (bpix / 8);
339 for (i = 0; i < height; ++i) {
341 for (j = 0; j < width; j++) {
342 write_pix8(fb, bpix, eformat, palette, bmap);
346 bmap += (padded_width - width);
347 fb -= byte_width + priv->line_length;
351 if (IS_ENABLED(CONFIG_BMP_16BPP)) {
352 for (i = 0; i < height; ++i) {
354 for (j = 0; j < width; j++) {
358 bmap += (padded_width - width);
359 fb -= width * 2 + priv->line_length;
364 if (IS_ENABLED(CONFIG_BMP_24BPP)) {
365 for (i = 0; i < height; ++i) {
366 for (j = 0; j < width; j++) {
368 /* 16bit 565RGB format */
369 *(u16 *)fb = ((bmap[2] >> 3)
371 ((bmap[1] >> 2) << 5) |
375 } else if (eformat == VIDEO_X2R10G10B10) {
379 pix |= *bmap++ << 12U;
380 pix |= *bmap++ << 22U;
382 *fb++ = (pix >> 8) & 0xff;
383 *fb++ = (pix >> 16) & 0xff;
392 fb -= priv->line_length + width * (bpix / 8);
393 bmap += (padded_width - width);
398 if (IS_ENABLED(CONFIG_BMP_32BPP)) {
399 for (i = 0; i < height; ++i) {
400 for (j = 0; j < width; j++) {
401 if (eformat == VIDEO_X2R10G10B10) {
405 pix |= *bmap++ << 12U;
406 pix |= *bmap++ << 22U;
407 pix |= (*bmap++ >> 6) << 30U;
409 *fb++ = (pix >> 8) & 0xff;
410 *fb++ = (pix >> 16) & 0xff;
419 fb -= priv->line_length + width * (bpix / 8);
427 /* 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);
433 return video_sync(dev, false);