psplash: really fix R&B bit swapping
[profile/ivi/psplash.git] / psplash-fb.c
1 /*
2  *  pslash - a lightweight framebuffer splashscreen for embedded devices.
3  *
4  *  Copyright (c) 2006 Matthew Allum <mallum@o-hand.com>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2, or (at your option)
9  *  any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  */
17 #include "psplash.h"
18
19 void
20 psplash_fb_destroy (PSplashFB *fb)
21 {
22   if (fb->fd >= 0)
23     close (fb->fd);
24
25   free(fb);
26 }
27
28 static int
29 attempt_to_change_pixel_format (PSplashFB *fb,
30                                 struct fb_var_screeninfo *fb_var)
31 {
32   /* By default the framebuffer driver may have set an oversized
33    * yres_virtual to support VT scrolling via the panning interface.
34    *
35    * We don't try and maintain this since it's more likely that we
36    * will fail to increase the bpp if the driver's pre allocated
37    * framebuffer isn't large enough.
38    */
39   fb_var->yres_virtual = fb_var->yres;
40
41   /* First try setting an 8,8,8,0 pixel format so we don't have to do
42    * any conversions while drawing. */
43
44   fb_var->bits_per_pixel = 32;
45
46   fb_var->red.offset = 0;
47   fb_var->red.length = 8;
48
49   fb_var->green.offset = 8;
50   fb_var->green.length = 8;
51
52   fb_var->blue.offset = 16;
53   fb_var->blue.length = 8;
54
55   fb_var->transp.offset = 0;
56   fb_var->transp.length = 0;
57
58   if (ioctl (fb->fd, FBIOPUT_VSCREENINFO, fb_var) == 0)
59     {
60       fprintf(stdout, "Switched to a 32 bpp 8,8,8 frame buffer\n");
61       return 1;
62     }
63   else
64     {
65       fprintf(stderr,
66               "Error, failed to switch to a 32 bpp 8,8,8 frame buffer\n");
67     }
68
69   /* Otherwise try a 16bpp 5,6,5 format */
70
71   fb_var->bits_per_pixel = 16;
72
73   fb_var->red.offset = 11;
74   fb_var->red.length = 5;
75
76   fb_var->green.offset = 5;
77   fb_var->green.length = 6;
78
79   fb_var->blue.offset = 0;
80   fb_var->blue.length = 5;
81
82   fb_var->transp.offset = 0;
83   fb_var->transp.length = 0;
84
85   if (ioctl (fb->fd, FBIOPUT_VSCREENINFO, fb_var) == 0)
86     {
87       fprintf(stdout, "Switched to a 16 bpp 5,6,5 frame buffer\n");
88       return 1;
89     }
90   else
91     {
92       fprintf(stderr,
93               "Error, failed to switch to a 16 bpp 5,6,5 frame buffer\n");
94     }
95
96   return 0;
97 }
98
99 PSplashFB*
100 psplash_fb_new (int angle)
101 {
102   struct fb_var_screeninfo fb_var;
103   struct fb_fix_screeninfo fb_fix;
104   int                      off;
105   char                    *fbdev;
106
107   PSplashFB *fb = NULL;
108
109   fbdev = getenv("FBDEV");
110   if (fbdev == NULL)
111     fbdev = "/dev/fb0";
112
113   if ((fb = malloc (sizeof(PSplashFB))) == NULL)
114     {
115       perror ("Error no memory");
116       goto fail;
117     }
118
119   memset (fb, 0, sizeof(PSplashFB));
120
121   fb->fd = -1;
122
123   if ((fb->fd = open (fbdev, O_RDWR)) < 0)
124     {
125       perror ("Error opening /dev/fb0");
126       goto fail;
127     }
128
129   if (ioctl (fb->fd, FBIOGET_VSCREENINFO, &fb_var) == -1)
130     {
131       perror ("Error getting variable framebuffer info");
132       goto fail;
133     }
134
135   if (fb_var.bits_per_pixel < 16)
136     {
137       fprintf(stderr,
138               "Error, no support currently for %i bpp frame buffers\n"
139               "Trying to change pixel format...\n",
140               fb_var.bits_per_pixel);
141       if (!attempt_to_change_pixel_format (fb, &fb_var))
142         goto fail;
143     }
144
145   /* NB: It looks like the fbdev concept of fixed vs variable screen info is
146    * broken. The line_length is part of the fixed info but it can be changed
147    * if you set a new pixel format. */
148   if (ioctl (fb->fd, FBIOGET_FSCREENINFO, &fb_fix) == -1)
149     {
150       perror ("Error getting fixed framebuffer info");
151       goto fail;
152     }
153
154   fb->real_width  = fb->width  = fb_var.xres;
155   fb->real_height = fb->height = fb_var.yres;
156   fb->bpp    = fb_var.bits_per_pixel;
157   fb->stride = fb_fix.line_length;
158   fb->type   = fb_fix.type;
159   fb->visual = fb_fix.visual;
160
161   DBG("width: %i, height: %i, bpp: %i, stride: %i",
162       fb->width, fb->height, fb->bpp, fb->stride);
163
164   fb->base = (char *) mmap ((caddr_t) NULL,
165                             /*fb_fix.smem_len */
166                             fb->stride * fb->height,
167                             PROT_READ|PROT_WRITE,
168                             MAP_SHARED,
169                             fb->fd, 0);
170
171   if (fb->base == (char *)-1)
172     {
173       perror("Error cannot mmap framebuffer ");
174       goto fail;
175     }
176
177   off = (unsigned long) fb_fix.smem_start % (unsigned long) getpagesize();
178
179   fb->data = fb->base + off;
180
181 #if 0
182   /* FIXME: No support for 8pp as yet  */
183   if (visual == FB_VISUAL_PSEUDOCOLOR
184       || visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
185   {
186     static struct fb_cmap cmap;
187
188     cmap.start = 0;
189     cmap.len = 16;
190     cmap.red = saved_red;
191     cmap.green = saved_green;
192     cmap.blue = saved_blue;
193     cmap.transp = NULL;
194
195     ioctl (fb, FBIOGETCMAP, &cmap);
196   }
197
198   if (!status)
199     atexit (bogl_done);
200   status = 2;
201 #endif
202
203   fb->angle = angle;
204
205   switch (fb->angle)
206     {
207     case 270:
208     case 90:
209       fb->width  = fb->real_height;
210       fb->height = fb->real_width;
211       break;
212     case 180:
213     case 0:
214     default:
215       break;
216     }
217
218   return fb;
219
220  fail:
221
222   if (fb)
223     psplash_fb_destroy (fb);
224
225   return NULL;
226 }
227
228 #define OFFSET(fb,x,y) (((y) * (fb)->stride) + ((x) * ((fb)->bpp >> 3)))
229
230 inline void
231 psplash_fb_plot_pixel (PSplashFB    *fb,
232                        int          x,
233                        int          y,
234                        uint8        red,
235                        uint8        green,
236                        uint8        blue)
237 {
238   int off;
239
240   if (x < 0 || x > fb->width-1 || y < 0 || y > fb->height-1)
241     return;
242
243   switch (fb->angle)
244     {
245     case 270:
246       off = OFFSET (fb, fb->height - y - 1, x);
247       break;
248     case 180:
249       off = OFFSET (fb, fb->width - x - 1, fb->height - y - 1);
250       break;
251     case 90:
252       off = OFFSET (fb, y, fb->width - x - 1);
253       break;
254     case 0:
255     default:
256       off = OFFSET (fb, x, y);
257       break;
258     }
259
260   /* FIXME: handle no RGB orderings */
261   switch (fb->bpp)
262     {
263     case 24:
264     case 32:
265       *(fb->data + off)     = red;
266       *(fb->data + off + 1) = green;
267       *(fb->data + off + 2) = blue;
268       break;
269     case 16:
270       *(volatile uint16 *) (fb->data + off)
271         = ((red >> 3) << 11) | ((green >> 2) << 5) | (blue >> 3);
272       break;
273     default:
274       /* depth not supported yet */
275       break;
276     }
277
278 }
279
280 void
281 psplash_fb_draw_rect (PSplashFB    *fb,
282                       int          x,
283                       int          y,
284                       int          width,
285                       int          height,
286                       uint8        red,
287                       uint8        green,
288                       uint8        blue)
289 {
290   int dx, dy;
291
292   for (dy=0; dy < height; dy++)
293     for (dx=0; dx < width; dx++)
294         psplash_fb_plot_pixel (fb, x+dx, y+dy, red, green, blue);
295 }
296
297 void
298 psplash_fb_draw_image (PSplashFB    *fb,
299                        int          x,
300                        int          y,
301                        int          img_width,
302                        int          img_height,
303                        int          img_bytes_per_pixel,
304                        uint8       *rle_data)
305 {
306   uint8       *p = rle_data;
307   int          dx = 0, dy = 0,  total_len;
308   unsigned int len;
309
310   total_len = img_width * img_height * img_bytes_per_pixel;
311
312   /* FIXME: Optimise, check for over runs ... */
313   while ((p - rle_data) < total_len)
314     {
315       len = *(p++);
316
317       if (len & 128)
318         {
319           len -= 128;
320
321           if (len == 0) break;
322
323           do
324             {
325               if (img_bytes_per_pixel < 4 || *(p+3))
326                 psplash_fb_plot_pixel (fb, x+dx, y+dy, *(p+2), *(p+1), *(p));
327               if (++dx >= img_width) { dx=0; dy++; }
328             }
329           while (--len && (p - rle_data) < total_len);
330
331           p += img_bytes_per_pixel;
332         }
333       else
334         {
335           if (len == 0) break;
336
337           do
338             {
339               if (img_bytes_per_pixel < 4 || *(p+3))
340                 psplash_fb_plot_pixel (fb, x+dx, y+dy, *(p+2), *(p+1), *(p));
341               if (++dx >= img_width) { dx=0; dy++; }
342               p += img_bytes_per_pixel;
343             }
344           while (--len && (p - rle_data) < total_len);
345         }
346     }
347 }
348
349 /* Font rendering code based on BOGL by Ben Pfaff */
350
351 static int
352 psplash_font_glyph (const PSplashFont *font, wchar_t wc, u_int32_t **bitmap)
353 {
354   int mask = font->index_mask;
355   int i;
356
357   for (;;)
358     {
359       for (i = font->offset[wc & mask]; font->index[i]; i += 2)
360         {
361           if ((font->index[i] & ~mask) == (wc & ~mask))
362             {
363               if (bitmap != NULL)
364                 *bitmap = &font->content[font->index[i+1]];
365               return font->index[i] & mask;
366             }
367         }
368     }
369   return 0;
370 }
371
372 void
373 psplash_fb_text_size (PSplashFB          *fb,
374                       int                *width,
375                       int                *height,
376                       const PSplashFont  *font,
377                       const char         *text)
378 {
379   char   *c = (char*)text;
380   wchar_t wc;
381   int     k, n, w, h, mw;
382
383   n = strlen (text);
384   mw = h = w = 0;
385
386   mbtowc (0, 0, 0);
387   for (; (k = mbtowc (&wc, c, n)) > 0; c += k, n -= k)
388     {
389       if (*c == '\n')
390         {
391           if (w > mw)
392             mw = 0;
393           h += font->height;
394           continue;
395         }
396
397       w += psplash_font_glyph (font, wc, NULL);
398     }
399
400   *width  = (w > mw) ? w : mw;
401   *height = (h == 0) ? font->height : h;
402 }
403
404 void
405 psplash_fb_draw_text (PSplashFB         *fb,
406                       int                x,
407                       int                y,
408                       uint8              red,
409                       uint8              green,
410                       uint8              blue,
411                       const PSplashFont *font,
412                       const char        *text)
413 {
414   int     h, w, k, n, cx, cy, dx, dy;
415   char   *c = (char*)text;
416   wchar_t wc;
417
418   n = strlen (text);
419   h = font->height;
420   dx = dy = 0;
421
422   mbtowc (0, 0, 0);
423   for (; (k = mbtowc (&wc, c, n)) > 0; c += k, n -= k)
424     {
425       u_int32_t *glyph = NULL;
426
427       if (*c == '\n')
428         {
429           dy += h;
430           dx  = 0;
431           continue;
432         }
433
434       w = psplash_font_glyph (font, wc, &glyph);
435
436       if (glyph == NULL)
437         continue;
438
439       for (cy = 0; cy < h; cy++)
440         {
441           u_int32_t g = *glyph++;
442
443           for (cx = 0; cx < w; cx++)
444             {
445               if (g & 0x80000000)
446                 psplash_fb_plot_pixel (fb, x+dx+cx, y+dy+cy,
447                                        red, green, blue);
448               g <<= 1;
449             }
450         }
451
452       dx += w;
453     }
454 }
455