video: theadorable: Use RGB565 for BMP blitting
[platform/kernel/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 #define BMP_RLE8_ESCAPE         0
17 #define BMP_RLE8_EOL            0
18 #define BMP_RLE8_EOBMP          1
19 #define BMP_RLE8_DELTA          2
20
21 /**
22  * get_bmp_col_16bpp() - Convert a colour-table entry into a 16bpp pixel value
23  *
24  * @return value to write to the 16bpp frame buffer for this palette entry
25  */
26 static 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  */
45 static 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
69 #ifdef CONFIG_VIDEO_BMP_RLE8
70 static void draw_unencoded_bitmap(u8 **fbp, uint bpix, uchar *bmap,
71                                   struct bmp_color_table_entry *palette,
72                                   int cnt)
73 {
74         u8 *fb = *fbp;
75
76         while (cnt > 0) {
77                 write_pix8(fb, bpix, palette, bmap++);
78                 fb += bpix / 8;
79                 cnt--;
80         }
81         *fbp = fb;
82 }
83
84 static void draw_encoded_bitmap(u8 **fbp, uint bpix,
85                                 struct bmp_color_table_entry *palette, u8 *bmap,
86                                 int cnt)
87 {
88         u8 *fb = *fbp;
89
90         while (cnt > 0) {
91                 write_pix8(fb, bpix, palette, bmap);
92                 fb += bpix / 8;
93                 cnt--;
94         }
95         *fbp = fb;
96 }
97
98 static void video_display_rle8_bitmap(struct udevice *dev,
99                                       struct bmp_image *bmp, uint bpix,
100                                       struct bmp_color_table_entry *palette,
101                                       uchar *fb, int x_off, int y_off,
102                                       ulong width, ulong height)
103 {
104         struct video_priv *priv = dev_get_uclass_priv(dev);
105         uchar *bmap;
106         ulong cnt, runlen;
107         int x, y;
108         int decode = 1;
109         uint bytes_per_pixel = bpix / 8;
110
111         debug("%s\n", __func__);
112         bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
113
114         x = 0;
115         y = height - 1;
116
117         while (decode) {
118                 if (bmap[0] == BMP_RLE8_ESCAPE) {
119                         switch (bmap[1]) {
120                         case BMP_RLE8_EOL:
121                                 /* end of line */
122                                 bmap += 2;
123                                 x = 0;
124                                 y--;
125                                 fb -= width * bytes_per_pixel +
126                                         priv->line_length;
127                                 break;
128                         case BMP_RLE8_EOBMP:
129                                 /* end of bitmap */
130                                 decode = 0;
131                                 break;
132                         case BMP_RLE8_DELTA:
133                                 /* delta run */
134                                 x += bmap[2];
135                                 y -= bmap[3];
136                                 fb = (uchar *)(priv->fb +
137                                         (y + y_off - 1) * priv->line_length +
138                                         (x + x_off) * bytes_per_pixel);
139                                 bmap += 4;
140                                 break;
141                         default:
142                                 /* unencoded run */
143                                 runlen = bmap[1];
144                                 bmap += 2;
145                                 if (y < height) {
146                                         if (x < width) {
147                                                 if (x + runlen > width)
148                                                         cnt = width - x;
149                                                 else
150                                                         cnt = runlen;
151                                                 draw_unencoded_bitmap(
152                                                         &fb, bpix,
153                                                         bmap, palette, cnt);
154                                         }
155                                         x += runlen;
156                                 }
157                                 bmap += runlen;
158                                 if (runlen & 1)
159                                         bmap++;
160                         }
161                 } else {
162                         /* encoded run */
163                         if (y < height) {
164                                 runlen = bmap[0];
165                                 if (x < width) {
166                                         /* aggregate the same code */
167                                         while (bmap[0] == 0xff &&
168                                                bmap[2] != BMP_RLE8_ESCAPE &&
169                                                bmap[1] == bmap[3]) {
170                                                 runlen += bmap[2];
171                                                 bmap += 2;
172                                         }
173                                         if (x + runlen > width)
174                                                 cnt = width - x;
175                                         else
176                                                 cnt = runlen;
177                                         draw_encoded_bitmap(&fb, bpix, palette,
178                                                             &bmap[1], cnt);
179                                 }
180                                 x += runlen;
181                         }
182                         bmap += 2;
183                 }
184         }
185 }
186 #endif
187
188 /**
189  * video_splash_align_axis() - Align a single coordinate
190  *
191  *- if a coordinate is 0x7fff then the image will be centred in
192  *  that direction
193  *- if a coordinate is -ve then it will be offset to the
194  *  left/top of the centre by that many pixels
195  *- if a coordinate is positive it will be used unchnaged.
196  *
197  * @axis:       Input and output coordinate
198  * @panel_size: Size of panel in pixels for that axis
199  * @picture_size:       Size of bitmap in pixels for that axis
200  */
201 static void video_splash_align_axis(int *axis, unsigned long panel_size,
202                                     unsigned long picture_size)
203 {
204         long panel_picture_delta = panel_size - picture_size;
205         long axis_alignment;
206
207         if (*axis == BMP_ALIGN_CENTER)
208                 axis_alignment = panel_picture_delta / 2;
209         else if (*axis < 0)
210                 axis_alignment = panel_picture_delta + *axis + 1;
211         else
212                 return;
213
214         *axis = max(0, (int)axis_alignment);
215 }
216
217 int video_bmp_display(struct udevice *dev, ulong bmp_image, int x, int y,
218                       bool align)
219 {
220         struct video_priv *priv = dev_get_uclass_priv(dev);
221         int i, j;
222         uchar *start, *fb;
223         struct bmp_image *bmp = map_sysmem(bmp_image, 0);
224         uchar *bmap;
225         ushort padded_width;
226         unsigned long width, height, byte_width;
227         unsigned long pwidth = priv->xsize;
228         unsigned colours, bpix, bmp_bpix;
229         struct bmp_color_table_entry *palette;
230         int hdr_size;
231         int ret;
232
233         if (!bmp || !(bmp->header.signature[0] == 'B' &&
234             bmp->header.signature[1] == 'M')) {
235                 printf("Error: no valid bmp image at %lx\n", bmp_image);
236
237                 return -EINVAL;
238         }
239
240         width = get_unaligned_le32(&bmp->header.width);
241         height = get_unaligned_le32(&bmp->header.height);
242         bmp_bpix = get_unaligned_le16(&bmp->header.bit_count);
243         hdr_size = get_unaligned_le16(&bmp->header.size);
244         debug("hdr_size=%d, bmp_bpix=%d\n", hdr_size, bmp_bpix);
245         palette = (void *)bmp + 14 + hdr_size;
246
247         colours = 1 << bmp_bpix;
248
249         bpix = VNBITS(priv->bpix);
250
251         if (bpix != 1 && bpix != 8 && bpix != 16 && bpix != 32) {
252                 printf("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
253                        bpix, bmp_bpix);
254
255                 return -EINVAL;
256         }
257
258         /*
259          * We support displaying 8bpp and 24bpp BMPs on 16bpp LCDs
260          * and displaying 24bpp BMPs on 32bpp LCDs
261          */
262         if (bpix != bmp_bpix &&
263             !(bmp_bpix == 8 && bpix == 16) &&
264             !(bmp_bpix == 8 && bpix == 24) &&
265             !(bmp_bpix == 8 && bpix == 32) &&
266             !(bmp_bpix == 24 && bpix == 16) &&
267             !(bmp_bpix == 24 && bpix == 32)) {
268                 printf("Error: %d bit/pixel mode, but BMP has %d bit/pixel\n",
269                        bpix, get_unaligned_le16(&bmp->header.bit_count));
270                 return -EPERM;
271         }
272
273         debug("Display-bmp: %d x %d  with %d colours, display %d\n",
274               (int)width, (int)height, (int)colours, 1 << bpix);
275
276         padded_width = (width & 0x3 ? (width & ~0x3) + 4 : width);
277
278         if (align) {
279                 video_splash_align_axis(&x, priv->xsize, width);
280                 video_splash_align_axis(&y, priv->ysize, height);
281         }
282
283         if ((x + width) > pwidth)
284                 width = pwidth - x;
285         if ((y + height) > priv->ysize)
286                 height = priv->ysize - y;
287
288         bmap = (uchar *)bmp + get_unaligned_le32(&bmp->header.data_offset);
289         start = (uchar *)(priv->fb +
290                 (y + height) * priv->line_length + x * bpix / 8);
291
292         /* Move back to the final line to be drawn */
293         fb = start - priv->line_length;
294
295         switch (bmp_bpix) {
296         case 1:
297         case 8: {
298 #ifdef CONFIG_VIDEO_BMP_RLE8
299                 u32 compression = get_unaligned_le32(&bmp->header.compression);
300                 debug("compressed %d %d\n", compression, BMP_BI_RLE8);
301                 if (compression == BMP_BI_RLE8) {
302                         video_display_rle8_bitmap(dev, bmp, bpix, palette, fb,
303                                                   x, y, width, height);
304                         break;
305                 }
306 #endif
307                 byte_width = width * (bpix / 8);
308                 if (!byte_width)
309                         byte_width = width;
310
311                 for (i = 0; i < height; ++i) {
312                         WATCHDOG_RESET();
313                         for (j = 0; j < width; j++) {
314                                 write_pix8(fb, bpix, palette, bmap);
315                                 bmap++;
316                                 fb += bpix / 8;
317                         }
318                         bmap += (padded_width - width);
319                         fb -= byte_width + priv->line_length;
320                 }
321                 break;
322         }
323 #if defined(CONFIG_BMP_16BPP)
324         case 16:
325                 for (i = 0; i < height; ++i) {
326                         WATCHDOG_RESET();
327                         for (j = 0; j < width; j++) {
328                                 *fb++ = *bmap++;
329                                 *fb++ = *bmap++;
330                         }
331                         bmap += (padded_width - width);
332                         fb -= width * 2 + priv->line_length;
333                 }
334                 break;
335 #endif /* CONFIG_BMP_16BPP */
336 #if defined(CONFIG_BMP_24BPP)
337         case 24:
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) << 11) |
343                                                 ((bmap[1] >> 2) << 5) |
344                                                 (bmap[0] >> 3);
345                                         bmap += 3;
346                                         fb += 2;
347                                 } else {
348                                         *fb++ = *bmap++;
349                                         *fb++ = *bmap++;
350                                         *fb++ = *bmap++;
351                                         *fb++ = 0;
352                                 }
353                         }
354                         fb -= priv->line_length + width * (bpix / 8);
355                         bmap += (padded_width - width);
356                 }
357                 break;
358 #endif /* CONFIG_BMP_24BPP */
359 #if defined(CONFIG_BMP_32BPP)
360         case 32:
361                 for (i = 0; i < height; ++i) {
362                         for (j = 0; j < width; j++) {
363                                 *fb++ = *bmap++;
364                                 *fb++ = *bmap++;
365                                 *fb++ = *bmap++;
366                                 *fb++ = *bmap++;
367                         }
368                         fb -= priv->line_length + width * (bpix / 8);
369                 }
370                 break;
371 #endif /* CONFIG_BMP_32BPP */
372         default:
373                 break;
374         };
375
376         /* Find the position of the top left of the image in the framebuffer */
377         fb = (uchar *)(priv->fb + y * priv->line_length + x * bpix / 8);
378         ret = video_sync_copy(dev, start, fb);
379         if (ret)
380                 return log_ret(ret);
381
382         return video_sync(dev, false);
383 }