Merge branch '2020-05-18-reduce-size-of-common.h'
[oweals/u-boot.git] / drivers / video / video_bmp.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2015 Google, Inc
4  */
5
6 #include <common.h>
7 #include <bmp_layout.h>
8 #include <dm.h>
9 #include <log.h>
10 #include <mapmem.h>
11 #include <splash.h>
12 #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
22 static 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
31 static 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
42 static void video_display_rle8_bitmap(struct udevice *dev,
43                                       struct bmp_image *bmp, ushort *cmap,
44                                       uchar *fb, int x_off, int y_off,
45                                       ulong width, ulong height)
46 {
47         struct video_priv *priv = dev_get_uclass_priv(dev);
48         uchar *bmap;
49         ulong cnt, runlen;
50         int x, y;
51         int decode = 1;
52
53         debug("%s\n", __func__);
54         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
143 /**
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  */
156 static void video_splash_align_axis(int *axis, unsigned long panel_size,
157                                     unsigned long picture_size)
158 {
159         long panel_picture_delta = panel_size - picture_size;
160         long axis_alignment;
161
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
172 static 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
189 int 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;
194         int i, j;
195         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         /*
231          * We support displaying 8bpp and 24bpp BMPs on 16bpp LCDs
232          * and displaying 24bpp BMPs on 32bpp LCDs
233          */
234         if (bpix != bmp_bpix &&
235             !(bmp_bpix == 8 && bpix == 16) &&
236             !(bmp_bpix == 24 && bpix == 16) &&
237             !(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,
279                                                   y, width, height);
280                         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;
313                         fb -= width * 2 + priv->line_length;
314                 }
315                 break;
316 #endif /* CONFIG_BMP_16BPP */
317 #if defined(CONFIG_BMP_24BPP)
318         case 24:
319                 for (i = 0; i < height; ++i) {
320                         for (j = 0; j < width; j++) {
321                                 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                                 }
334                         }
335                         fb -= priv->line_length + width * (bpix / 8);
336                         bmap += (padded_width - width) * 3;
337                 }
338                 break;
339 #endif /* CONFIG_BMP_24BPP */
340 #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                         }
349                         fb -= priv->line_length + width * (bpix / 8);
350                 }
351                 break;
352 #endif /* CONFIG_BMP_32BPP */
353         default:
354                 break;
355         };
356
357         video_sync(dev, false);
358
359         return 0;
360 }
361