blob: eb9636541d7c565f99c026c9142745c22e3e256b [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
16#ifdef CONFIG_VIDEO_BMP_RLE8
17#define BMP_RLE8_ESCAPE 0
18#define BMP_RLE8_EOL 0
19#define BMP_RLE8_EOBMP 1
20#define BMP_RLE8_DELTA 2
21
22static void draw_unencoded_bitmap(ushort **fbp, uchar *bmap, ushort *cmap,
23 int cnt)
24{
25 while (cnt > 0) {
26 *(*fbp)++ = cmap[*bmap++];
27 cnt--;
28 }
29}
30
31static void draw_encoded_bitmap(ushort **fbp, ushort col, int cnt)
32{
33 ushort *fb = *fbp;
34
35 while (cnt > 0) {
36 *fb++ = col;
37 cnt--;
38 }
39 *fbp = fb;
40}
41
42static void video_display_rle8_bitmap(struct udevice *dev,
43 struct bmp_image *bmp, ushort *cmap,
Patrice Chotardcb30f7d2019-11-20 14:11:16 +010044 uchar *fb, int x_off, int y_off,
45 ulong width, ulong height)
Simon Glass68b26272016-01-18 19:52:22 -070046{
47 struct video_priv *priv = dev_get_uclass_priv(dev);
48 uchar *bmap;
Simon Glass68b26272016-01-18 19:52:22 -070049 ulong cnt, runlen;
50 int x, y;
51 int decode = 1;
52
53 debug("%s\n", __func__);
Simon Glass68b26272016-01-18 19:52:22 -070054 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
55
56 x = 0;
57 y = height - 1;
58
59 while (decode) {
60 if (bmap[0] == BMP_RLE8_ESCAPE) {
61 switch (bmap[1]) {
62 case BMP_RLE8_EOL:
63 /* end of line */
64 bmap += 2;
65 x = 0;
66 y--;
67 /* 16bpix, 2-byte per pixel, width should *2 */
68 fb -= (width * 2 + priv->line_length);
69 break;
70 case BMP_RLE8_EOBMP:
71 /* end of bitmap */
72 decode = 0;
73 break;
74 case BMP_RLE8_DELTA:
75 /* delta run */
76 x += bmap[2];
77 y -= bmap[3];
78 /* 16bpix, 2-byte per pixel, x should *2 */
79 fb = (uchar *)(priv->fb + (y + y_off - 1)
80 * priv->line_length + (x + x_off) * 2);
81 bmap += 4;
82 break;
83 default:
84 /* unencoded run */
85 runlen = bmap[1];
86 bmap += 2;
87 if (y < height) {
88 if (x < width) {
89 if (x + runlen > width)
90 cnt = width - x;
91 else
92 cnt = runlen;
93 draw_unencoded_bitmap(
94 (ushort **)&fb,
95 bmap, cmap, cnt);
96 }
97 x += runlen;
98 }
99 bmap += runlen;
100 if (runlen & 1)
101 bmap++;
102 }
103 } else {
104 /* encoded run */
105 if (y < height) {
106 runlen = bmap[0];
107 if (x < width) {
108 /* aggregate the same code */
109 while (bmap[0] == 0xff &&
110 bmap[2] != BMP_RLE8_ESCAPE &&
111 bmap[1] == bmap[3]) {
112 runlen += bmap[2];
113 bmap += 2;
114 }
115 if (x + runlen > width)
116 cnt = width - x;
117 else
118 cnt = runlen;
119 draw_encoded_bitmap((ushort **)&fb,
120 cmap[bmap[1]], cnt);
121 }
122 x += runlen;
123 }
124 bmap += 2;
125 }
126 }
127}
128#endif
129
130__weak void fb_put_byte(uchar **fb, uchar **from)
131{
132 *(*fb)++ = *(*from)++;
133}
134
135#if defined(CONFIG_BMP_16BPP)
136__weak void fb_put_word(uchar **fb, uchar **from)
137{
138 *(*fb)++ = *(*from)++;
139 *(*fb)++ = *(*from)++;
140}
141#endif /* CONFIG_BMP_16BPP */
142
Simon Glass68b26272016-01-18 19:52:22 -0700143/**
144 * video_splash_align_axis() - Align a single coordinate
145 *
146 *- if a coordinate is 0x7fff then the image will be centred in
147 * that direction
148 *- if a coordinate is -ve then it will be offset to the
149 * left/top of the centre by that many pixels
150 *- if a coordinate is positive it will be used unchnaged.
151 *
152 * @axis: Input and output coordinate
153 * @panel_size: Size of panel in pixels for that axis
154 * @picture_size: Size of bitmap in pixels for that axis
155 */
156static void video_splash_align_axis(int *axis, unsigned long panel_size,
157 unsigned long picture_size)
158{
Patrice Chotard066eace2019-11-20 14:11:15 +0100159 long panel_picture_delta = panel_size - picture_size;
160 long axis_alignment;
Simon Glass68b26272016-01-18 19:52:22 -0700161
162 if (*axis == BMP_ALIGN_CENTER)
163 axis_alignment = panel_picture_delta / 2;
164 else if (*axis < 0)
165 axis_alignment = panel_picture_delta + *axis + 1;
166 else
167 return;
168
169 *axis = max(0, (int)axis_alignment);
170}
171
172static void video_set_cmap(struct udevice *dev,
173 struct bmp_color_table_entry *cte, unsigned colours)
174{
175 struct video_priv *priv = dev_get_uclass_priv(dev);
176 int i;
177 ushort *cmap = priv->cmap;
178
179 debug("%s: colours=%d\n", __func__, colours);
180 for (i = 0; i < colours; ++i) {
181 *cmap = ((cte->red << 8) & 0xf800) |
182 ((cte->green << 3) & 0x07e0) |
183 ((cte->blue >> 3) & 0x001f);
184 cmap++;
185 cte++;
186 }
187}
188
189int video_bmp_display(struct udevice *dev, ulong bmp_image, int x, int y,
190 bool align)
191{
192 struct video_priv *priv = dev_get_uclass_priv(dev);
193 ushort *cmap_base = NULL;
Simon Glass1f475732016-01-30 15:45:16 -0700194 int i, j;
Simon Glass68b26272016-01-18 19:52:22 -0700195 uchar *fb;
196 struct bmp_image *bmp = map_sysmem(bmp_image, 0);
197 uchar *bmap;
198 ushort padded_width;
199 unsigned long width, height, byte_width;
200 unsigned long pwidth = priv->xsize;
201 unsigned colours, bpix, bmp_bpix;
202 struct bmp_color_table_entry *palette;
203 int hdr_size;
204
205 if (!bmp || !(bmp->header.signature[0] == 'B' &&
206 bmp->header.signature[1] == 'M')) {
207 printf("Error: no valid bmp image at %lx\n", bmp_image);
208
209 return -EINVAL;
210 }
211
212 width = get_unaligned_le32(&bmp->header.width);
213 height = get_unaligned_le32(&bmp->header.height);
214 bmp_bpix = get_unaligned_le16(&bmp->header.bit_count);
215 hdr_size = get_unaligned_le16(&bmp->header.size);
216 debug("hdr_size=%d, bmp_bpix=%d\n", hdr_size, bmp_bpix);
217 palette = (void *)bmp + 14 + hdr_size;
218
219 colours = 1 << bmp_bpix;
220
221 bpix = VNBITS(priv->bpix);
222
223 if (bpix != 1 && bpix != 8 && bpix != 16 && bpix != 32) {
224 printf("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
225 bpix, bmp_bpix);
226
227 return -EINVAL;
228 }
229
230 /*
Stefan Roese7ee599e2019-01-30 08:54:12 +0100231 * We support displaying 8bpp and 24bpp BMPs on 16bpp LCDs
Simon Glass68b26272016-01-18 19:52:22 -0700232 * and displaying 24bpp BMPs on 32bpp LCDs
Stefan Roese7ee599e2019-01-30 08:54:12 +0100233 */
Simon Glass68b26272016-01-18 19:52:22 -0700234 if (bpix != bmp_bpix &&
235 !(bmp_bpix == 8 && bpix == 16) &&
Stefan Roese7ee599e2019-01-30 08:54:12 +0100236 !(bmp_bpix == 24 && bpix == 16) &&
Simon Glass68b26272016-01-18 19:52:22 -0700237 !(bmp_bpix == 24 && bpix == 32)) {
238 printf("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
239 bpix, get_unaligned_le16(&bmp->header.bit_count));
240 return -EPERM;
241 }
242
243 debug("Display-bmp: %d x %d with %d colours, display %d\n",
244 (int)width, (int)height, (int)colours, 1 << bpix);
245
246 if (bmp_bpix == 8)
247 video_set_cmap(dev, palette, colours);
248
249 padded_width = (width & 0x3 ? (width & ~0x3) + 4 : width);
250
251 if (align) {
252 video_splash_align_axis(&x, priv->xsize, width);
253 video_splash_align_axis(&y, priv->ysize, height);
254 }
255
256 if ((x + width) > pwidth)
257 width = pwidth - x;
258 if ((y + height) > priv->ysize)
259 height = priv->ysize - y;
260
261 bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
262 fb = (uchar *)(priv->fb +
263 (y + height - 1) * priv->line_length + x * bpix / 8);
264
265 switch (bmp_bpix) {
266 case 1:
267 case 8: {
268 cmap_base = priv->cmap;
269#ifdef CONFIG_VIDEO_BMP_RLE8
270 u32 compression = get_unaligned_le32(&bmp->header.compression);
271 debug("compressed %d %d\n", compression, BMP_BI_RLE8);
272 if (compression == BMP_BI_RLE8) {
273 if (bpix != 16) {
274 /* TODO implement render code for bpix != 16 */
275 printf("Error: only support 16 bpix");
276 return -EPROTONOSUPPORT;
277 }
278 video_display_rle8_bitmap(dev, bmp, cmap_base, fb, x,
Patrice Chotardcb30f7d2019-11-20 14:11:16 +0100279 y, width, height);
Simon Glass68b26272016-01-18 19:52:22 -0700280 break;
281 }
282#endif
283
284 if (bpix != 16)
285 byte_width = width;
286 else
287 byte_width = width * 2;
288
289 for (i = 0; i < height; ++i) {
290 WATCHDOG_RESET();
291 for (j = 0; j < width; j++) {
292 if (bpix != 16) {
293 fb_put_byte(&fb, &bmap);
294 } else {
295 *(uint16_t *)fb = cmap_base[*bmap];
296 bmap++;
297 fb += sizeof(uint16_t) / sizeof(*fb);
298 }
299 }
300 bmap += (padded_width - width);
301 fb -= byte_width + priv->line_length;
302 }
303 break;
304 }
305#if defined(CONFIG_BMP_16BPP)
306 case 16:
307 for (i = 0; i < height; ++i) {
308 WATCHDOG_RESET();
309 for (j = 0; j < width; j++)
310 fb_put_word(&fb, &bmap);
311
312 bmap += (padded_width - width) * 2;
Stefan Roese956e3392016-11-12 10:32:38 +0100313 fb -= width * 2 + priv->line_length;
Simon Glass68b26272016-01-18 19:52:22 -0700314 }
315 break;
316#endif /* CONFIG_BMP_16BPP */
Philipp Tomsich5cfa3f42017-05-05 21:48:30 +0200317#if defined(CONFIG_BMP_24BPP)
Simon Glass68b26272016-01-18 19:52:22 -0700318 case 24:
319 for (i = 0; i < height; ++i) {
320 for (j = 0; j < width; j++) {
Stefan Roese7ee599e2019-01-30 08:54:12 +0100321 if (bpix == 16) {
322 /* 16bit 555RGB format */
323 *(u16 *)fb = ((bmap[2] >> 3) << 10) |
324 ((bmap[1] >> 3) << 5) |
325 (bmap[0] >> 3);
326 bmap += 3;
327 fb += 2;
328 } else {
329 *(fb++) = *(bmap++);
330 *(fb++) = *(bmap++);
331 *(fb++) = *(bmap++);
332 *(fb++) = 0;
333 }
Simon Glass68b26272016-01-18 19:52:22 -0700334 }
Stefan Roese956e3392016-11-12 10:32:38 +0100335 fb -= priv->line_length + width * (bpix / 8);
Stefan Roese7ee599e2019-01-30 08:54:12 +0100336 bmap += (padded_width - width) * 3;
Simon Glass68b26272016-01-18 19:52:22 -0700337 }
338 break;
Philipp Tomsich5cfa3f42017-05-05 21:48:30 +0200339#endif /* CONFIG_BMP_24BPP */
Simon Glass68b26272016-01-18 19:52:22 -0700340#if defined(CONFIG_BMP_32BPP)
341 case 32:
342 for (i = 0; i < height; ++i) {
343 for (j = 0; j < width; j++) {
344 *(fb++) = *(bmap++);
345 *(fb++) = *(bmap++);
346 *(fb++) = *(bmap++);
347 *(fb++) = *(bmap++);
348 }
Stefan Roese956e3392016-11-12 10:32:38 +0100349 fb -= priv->line_length + width * (bpix / 8);
Simon Glass68b26272016-01-18 19:52:22 -0700350 }
351 break;
352#endif /* CONFIG_BMP_32BPP */
353 default:
354 break;
355 };
356
Simon Glass0806dcc2018-10-01 11:55:14 -0600357 video_sync(dev, false);
Simon Glass68b26272016-01-18 19:52:22 -0700358
359 return 0;
360}
361