blob: c8c3fd3549dbb3be5f03bcbbb715e3a41642d08a [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/**
34 * write_pix8() - Write a pixel from a BMP image into the framebuffer
35 *
36 * This handles frame buffers with 8, 16, 24 or 32 bits per pixel
37 *
38 * @fb: Place in frame buffer to update
39 * @bpix: Frame buffer bits-per-pixel, which controls how many bytes are written
40 * @palette: BMP palette table
41 * @bmap: Pointer to BMP bitmap position to write. This contains a single byte
42 * which is either written directly (bpix == 8) or used to look up the
43 * palette to get a colour to write
44 */
45static void write_pix8(u8 *fb, uint bpix, struct bmp_color_table_entry *palette,
46 u8 *bmap)
47{
48 if (bpix == 8) {
49 *fb++ = *bmap;
50 } else if (bpix == 16) {
51 *(u16 *)fb = get_bmp_col_16bpp(palette[*bmap]);
52 } else {
53 /* Only support big endian */
54 struct bmp_color_table_entry *cte = &palette[*bmap];
55
56 if (bpix == 24) {
57 *fb++ = cte->red;
58 *fb++ = cte->green;
59 *fb++ = cte->blue;
60 } else {
61 *fb++ = cte->blue;
62 *fb++ = cte->green;
63 *fb++ = cte->red;
64 *fb++ = 0;
65 }
66 }
67}
68
Simon Glass646e1692021-11-19 13:23:55 -070069static void draw_unencoded_bitmap(u8 **fbp, uint bpix, uchar *bmap,
70 struct bmp_color_table_entry *palette,
Simon Glassb01c7922016-01-18 19:52:22 -070071 int cnt)
72{
Simon Glass646e1692021-11-19 13:23:55 -070073 u8 *fb = *fbp;
74
Simon Glassb01c7922016-01-18 19:52:22 -070075 while (cnt > 0) {
Simon Glass646e1692021-11-19 13:23:55 -070076 write_pix8(fb, bpix, palette, bmap++);
77 fb += bpix / 8;
Simon Glassb01c7922016-01-18 19:52:22 -070078 cnt--;
79 }
Simon Glass646e1692021-11-19 13:23:55 -070080 *fbp = fb;
Simon Glassb01c7922016-01-18 19:52:22 -070081}
82
Simon Glass646e1692021-11-19 13:23:55 -070083static void draw_encoded_bitmap(u8 **fbp, uint bpix,
84 struct bmp_color_table_entry *palette, u8 *bmap,
85 int cnt)
Simon Glassb01c7922016-01-18 19:52:22 -070086{
Simon Glass646e1692021-11-19 13:23:55 -070087 u8 *fb = *fbp;
Simon Glassb01c7922016-01-18 19:52:22 -070088
89 while (cnt > 0) {
Simon Glass646e1692021-11-19 13:23:55 -070090 write_pix8(fb, bpix, palette, bmap);
91 fb += bpix / 8;
Simon Glassb01c7922016-01-18 19:52:22 -070092 cnt--;
93 }
94 *fbp = fb;
95}
96
97static void video_display_rle8_bitmap(struct udevice *dev,
Simon Glass646e1692021-11-19 13:23:55 -070098 struct bmp_image *bmp, uint bpix,
99 struct bmp_color_table_entry *palette,
Patrice Chotardca2c6942019-11-20 14:11:16 +0100100 uchar *fb, int x_off, int y_off,
101 ulong width, ulong height)
Simon Glassb01c7922016-01-18 19:52:22 -0700102{
103 struct video_priv *priv = dev_get_uclass_priv(dev);
104 uchar *bmap;
Simon Glassb01c7922016-01-18 19:52:22 -0700105 ulong cnt, runlen;
106 int x, y;
107 int decode = 1;
Simon Glass646e1692021-11-19 13:23:55 -0700108 uint bytes_per_pixel = bpix / 8;
Simon Glassb01c7922016-01-18 19:52:22 -0700109
110 debug("%s\n", __func__);
Simon Glassb01c7922016-01-18 19:52:22 -0700111 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
112
113 x = 0;
114 y = height - 1;
115
116 while (decode) {
117 if (bmap[0] == BMP_RLE8_ESCAPE) {
118 switch (bmap[1]) {
119 case BMP_RLE8_EOL:
120 /* end of line */
121 bmap += 2;
122 x = 0;
123 y--;
Simon Glass646e1692021-11-19 13:23:55 -0700124 fb -= width * bytes_per_pixel +
125 priv->line_length;
Simon Glassb01c7922016-01-18 19:52:22 -0700126 break;
127 case BMP_RLE8_EOBMP:
128 /* end of bitmap */
129 decode = 0;
130 break;
131 case BMP_RLE8_DELTA:
132 /* delta run */
133 x += bmap[2];
134 y -= bmap[3];
Simon Glass646e1692021-11-19 13:23:55 -0700135 fb = (uchar *)(priv->fb +
136 (y + y_off - 1) * priv->line_length +
137 (x + x_off) * bytes_per_pixel);
Simon Glassb01c7922016-01-18 19:52:22 -0700138 bmap += 4;
139 break;
140 default:
141 /* unencoded run */
142 runlen = bmap[1];
143 bmap += 2;
144 if (y < height) {
145 if (x < width) {
146 if (x + runlen > width)
147 cnt = width - x;
148 else
149 cnt = runlen;
150 draw_unencoded_bitmap(
Simon Glass646e1692021-11-19 13:23:55 -0700151 &fb, bpix,
152 bmap, palette, cnt);
Simon Glassb01c7922016-01-18 19:52:22 -0700153 }
154 x += runlen;
155 }
156 bmap += runlen;
157 if (runlen & 1)
158 bmap++;
159 }
160 } else {
161 /* encoded run */
162 if (y < height) {
163 runlen = bmap[0];
164 if (x < width) {
165 /* aggregate the same code */
166 while (bmap[0] == 0xff &&
167 bmap[2] != BMP_RLE8_ESCAPE &&
168 bmap[1] == bmap[3]) {
169 runlen += bmap[2];
170 bmap += 2;
171 }
172 if (x + runlen > width)
173 cnt = width - x;
174 else
175 cnt = runlen;
Simon Glass646e1692021-11-19 13:23:55 -0700176 draw_encoded_bitmap(&fb, bpix, palette,
177 &bmap[1], cnt);
Simon Glassb01c7922016-01-18 19:52:22 -0700178 }
179 x += runlen;
180 }
181 bmap += 2;
182 }
183 }
184}
Simon Glassb01c7922016-01-18 19:52:22 -0700185
Simon Glassb01c7922016-01-18 19:52:22 -0700186/**
187 * video_splash_align_axis() - Align a single coordinate
188 *
189 *- if a coordinate is 0x7fff then the image will be centred in
190 * that direction
191 *- if a coordinate is -ve then it will be offset to the
192 * left/top of the centre by that many pixels
193 *- if a coordinate is positive it will be used unchnaged.
194 *
195 * @axis: Input and output coordinate
196 * @panel_size: Size of panel in pixels for that axis
197 * @picture_size: Size of bitmap in pixels for that axis
198 */
199static void video_splash_align_axis(int *axis, unsigned long panel_size,
200 unsigned long picture_size)
201{
Patrice Chotard1ebf2852019-11-20 14:11:15 +0100202 long panel_picture_delta = panel_size - picture_size;
203 long axis_alignment;
Simon Glassb01c7922016-01-18 19:52:22 -0700204
205 if (*axis == BMP_ALIGN_CENTER)
206 axis_alignment = panel_picture_delta / 2;
207 else if (*axis < 0)
208 axis_alignment = panel_picture_delta + *axis + 1;
209 else
210 return;
211
212 *axis = max(0, (int)axis_alignment);
213}
214
Simon Glassb01c7922016-01-18 19:52:22 -0700215int video_bmp_display(struct udevice *dev, ulong bmp_image, int x, int y,
216 bool align)
217{
218 struct video_priv *priv = dev_get_uclass_priv(dev);
Simon Glass2b80b4e2016-01-30 15:45:16 -0700219 int i, j;
Simon Glass2b1412c2020-07-02 21:12:27 -0600220 uchar *start, *fb;
Simon Glassb01c7922016-01-18 19:52:22 -0700221 struct bmp_image *bmp = map_sysmem(bmp_image, 0);
222 uchar *bmap;
223 ushort padded_width;
224 unsigned long width, height, byte_width;
225 unsigned long pwidth = priv->xsize;
226 unsigned colours, bpix, bmp_bpix;
227 struct bmp_color_table_entry *palette;
228 int hdr_size;
Simon Glass2b1412c2020-07-02 21:12:27 -0600229 int ret;
Simon Glassb01c7922016-01-18 19:52:22 -0700230
231 if (!bmp || !(bmp->header.signature[0] == 'B' &&
232 bmp->header.signature[1] == 'M')) {
233 printf("Error: no valid bmp image at %lx\n", bmp_image);
234
235 return -EINVAL;
236 }
237
238 width = get_unaligned_le32(&bmp->header.width);
239 height = get_unaligned_le32(&bmp->header.height);
240 bmp_bpix = get_unaligned_le16(&bmp->header.bit_count);
241 hdr_size = get_unaligned_le16(&bmp->header.size);
242 debug("hdr_size=%d, bmp_bpix=%d\n", hdr_size, bmp_bpix);
243 palette = (void *)bmp + 14 + hdr_size;
244
245 colours = 1 << bmp_bpix;
246
247 bpix = VNBITS(priv->bpix);
248
249 if (bpix != 1 && bpix != 8 && bpix != 16 && bpix != 32) {
250 printf("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
251 bpix, bmp_bpix);
252
253 return -EINVAL;
254 }
255
256 /*
Stefan Roesee2571032019-01-30 08:54:12 +0100257 * We support displaying 8bpp and 24bpp BMPs on 16bpp LCDs
Simon Glassb01c7922016-01-18 19:52:22 -0700258 * and displaying 24bpp BMPs on 32bpp LCDs
Stefan Roesee2571032019-01-30 08:54:12 +0100259 */
Simon Glassb01c7922016-01-18 19:52:22 -0700260 if (bpix != bmp_bpix &&
261 !(bmp_bpix == 8 && bpix == 16) &&
Ye Libab68b22020-06-10 02:52:23 -0700262 !(bmp_bpix == 8 && bpix == 24) &&
263 !(bmp_bpix == 8 && bpix == 32) &&
Stefan Roesee2571032019-01-30 08:54:12 +0100264 !(bmp_bpix == 24 && bpix == 16) &&
Simon Glassb01c7922016-01-18 19:52:22 -0700265 !(bmp_bpix == 24 && bpix == 32)) {
266 printf("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
267 bpix, get_unaligned_le16(&bmp->header.bit_count));
268 return -EPERM;
269 }
270
271 debug("Display-bmp: %d x %d with %d colours, display %d\n",
272 (int)width, (int)height, (int)colours, 1 << bpix);
273
Simon Glassb01c7922016-01-18 19:52:22 -0700274 padded_width = (width & 0x3 ? (width & ~0x3) + 4 : width);
275
276 if (align) {
277 video_splash_align_axis(&x, priv->xsize, width);
278 video_splash_align_axis(&y, priv->ysize, height);
279 }
280
281 if ((x + width) > pwidth)
282 width = pwidth - x;
283 if ((y + height) > priv->ysize)
284 height = priv->ysize - y;
285
286 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
Simon Glass2b1412c2020-07-02 21:12:27 -0600287 start = (uchar *)(priv->fb +
288 (y + height) * priv->line_length + x * bpix / 8);
289
290 /* Move back to the final line to be drawn */
291 fb = start - priv->line_length;
Simon Glassb01c7922016-01-18 19:52:22 -0700292
293 switch (bmp_bpix) {
294 case 1:
Simon Glasscd4fb0f2021-11-19 13:24:00 -0700295 case 8:
296 if (IS_ENABLED(CONFIG_VIDEO_BMP_RLE8)) {
297 u32 compression = get_unaligned_le32(
298 &bmp->header.compression);
299 debug("compressed %d %d\n", compression, BMP_BI_RLE8);
300 if (compression == BMP_BI_RLE8) {
301 video_display_rle8_bitmap(dev, bmp, bpix, palette, fb,
302 x, y, width, height);
303 break;
304 }
Simon Glassb01c7922016-01-18 19:52:22 -0700305 }
Simon Glasscd4fb0f2021-11-19 13:24:00 -0700306
307 /* Not compressed */
Ye Libab68b22020-06-10 02:52:23 -0700308 byte_width = width * (bpix / 8);
309 if (!byte_width)
Simon Glassb01c7922016-01-18 19:52:22 -0700310 byte_width = width;
Simon Glassb01c7922016-01-18 19:52:22 -0700311
312 for (i = 0; i < height; ++i) {
313 WATCHDOG_RESET();
314 for (j = 0; j < width; j++) {
Simon Glass51f92c12021-11-19 13:23:54 -0700315 write_pix8(fb, bpix, palette, bmap);
316 bmap++;
317 fb += bpix / 8;
Simon Glassb01c7922016-01-18 19:52:22 -0700318 }
319 bmap += (padded_width - width);
320 fb -= byte_width + priv->line_length;
321 }
322 break;
Simon Glassb01c7922016-01-18 19:52:22 -0700323 case 16:
Simon Glasscd4fb0f2021-11-19 13:24:00 -0700324 if (IS_ENABLED(CONFIG_BMP_16BPP)) {
325 for (i = 0; i < height; ++i) {
326 WATCHDOG_RESET();
327 for (j = 0; j < width; j++) {
Simon Glassf5aa93e2021-11-19 13:23:57 -0700328 *fb++ = *bmap++;
329 *fb++ = *bmap++;
Stefan Roesee2571032019-01-30 08:54:12 +0100330 }
Simon Glasscd4fb0f2021-11-19 13:24:00 -0700331 bmap += (padded_width - width);
332 fb -= width * 2 + priv->line_length;
Simon Glassb01c7922016-01-18 19:52:22 -0700333 }
Simon Glassb01c7922016-01-18 19:52:22 -0700334 }
335 break;
Simon Glasscd4fb0f2021-11-19 13:24:00 -0700336 case 24:
337 if (IS_ENABLED(CONFIG_BMP_24BPP)) {
338 for (i = 0; i < height; ++i) {
339 for (j = 0; j < width; j++) {
340 if (bpix == 16) {
341 /* 16bit 565RGB format */
342 *(u16 *)fb = ((bmap[2] >> 3)
343 << 11) |
344 ((bmap[1] >> 2) << 5) |
345 (bmap[0] >> 3);
346 bmap += 3;
347 fb += 2;
348 } else {
349 *fb++ = *bmap++;
350 *fb++ = *bmap++;
351 *fb++ = *bmap++;
352 *fb++ = 0;
353 }
354 }
355 fb -= priv->line_length + width * (bpix / 8);
356 bmap += (padded_width - width);
357 }
358 }
359 break;
Simon Glassb01c7922016-01-18 19:52:22 -0700360 case 32:
Simon Glasscd4fb0f2021-11-19 13:24:00 -0700361 if (IS_ENABLED(CONFIG_BMP_32BPP)) {
362 for (i = 0; i < height; ++i) {
363 for (j = 0; j < width; j++) {
364 *fb++ = *bmap++;
365 *fb++ = *bmap++;
366 *fb++ = *bmap++;
367 *fb++ = *bmap++;
368 }
369 fb -= priv->line_length + width * (bpix / 8);
Simon Glassb01c7922016-01-18 19:52:22 -0700370 }
Simon Glassb01c7922016-01-18 19:52:22 -0700371 }
372 break;
Simon Glassb01c7922016-01-18 19:52:22 -0700373 default:
374 break;
375 };
376
Simon Glass2b1412c2020-07-02 21:12:27 -0600377 /* Find the position of the top left of the image in the framebuffer */
378 fb = (uchar *)(priv->fb + y * priv->line_length + x * bpix / 8);
379 ret = video_sync_copy(dev, start, fb);
380 if (ret)
381 return log_ret(ret);
382
Michal Simek9de731f2020-12-14 08:47:52 +0100383 return video_sync(dev, false);
Simon Glassb01c7922016-01-18 19:52:22 -0700384}