2008-05-27 Robert Bragg <bob@o-hand.com>
[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               psplash_fb_plot_pixel (fb, x+dx, y+dy, *p, *(p+1), *(p+2));
326               if (++dx >= img_width) { dx=0; dy++; }
327             }
328           while (--len && (p - rle_data) < total_len);
329
330           p += img_bytes_per_pixel;
331         }
332       else
333         {
334           if (len == 0) break;
335
336           do
337             {
338               psplash_fb_plot_pixel (fb, x+dx, y+dy, *p, *(p+1), *(p+2));
339               if (++dx >= img_width) { dx=0; dy++; }
340               p += img_bytes_per_pixel;
341             }
342           while (--len && (p - rle_data) < total_len);
343         }
344     }
345 }
346
347 /* Font rendering code based on BOGL by Ben Pfaff */
348
349 static int
350 psplash_font_glyph (const PSplashFont *font, wchar_t wc, u_int32_t **bitmap)
351 {
352   int mask = font->index_mask;
353   int i;
354
355   for (;;)
356     {
357       for (i = font->offset[wc & mask]; font->index[i]; i += 2)
358         {
359           if ((font->index[i] & ~mask) == (wc & ~mask))
360             {
361               if (bitmap != NULL)
362                 *bitmap = &font->content[font->index[i+1]];
363               return font->index[i] & mask;
364             }
365         }
366     }
367   return 0;
368 }
369
370 void
371 psplash_fb_text_size (PSplashFB          *fb,
372                       int                *width,
373                       int                *height,
374                       const PSplashFont  *font,
375                       const char         *text)
376 {
377   char   *c = (char*)text;
378   wchar_t wc;
379   int     k, n, w, h, mw;
380
381   n = strlen (text);
382   mw = h = w = 0;
383
384   mbtowc (0, 0, 0);
385   for (; (k = mbtowc (&wc, c, n)) > 0; c += k, n -= k)
386     {
387       if (*c == '\n')
388         {
389           if (w > mw)
390             mw = 0;
391           h += font->height;
392           continue;
393         }
394
395       w += psplash_font_glyph (font, wc, NULL);
396     }
397
398   *width  = (w > mw) ? w : mw;
399   *height = (h == 0) ? font->height : h;
400 }
401
402 void
403 psplash_fb_draw_text (PSplashFB         *fb,
404                       int                x,
405                       int                y,
406                       uint8              red,
407                       uint8              green,
408                       uint8              blue,
409                       const PSplashFont *font,
410                       const char        *text)
411 {
412   int     h, w, k, n, cx, cy, dx, dy;
413   char   *c = (char*)text;
414   wchar_t wc;
415
416   n = strlen (text);
417   h = font->height;
418   dx = dy = 0;
419
420   mbtowc (0, 0, 0);
421   for (; (k = mbtowc (&wc, c, n)) > 0; c += k, n -= k)
422     {
423       u_int32_t *glyph = NULL;
424
425       if (*c == '\n')
426         {
427           dy += h;
428           dx  = 0;
429           continue;
430         }
431
432       w = psplash_font_glyph (font, wc, &glyph);
433
434       if (glyph == NULL)
435         continue;
436
437       for (cy = 0; cy < h; cy++)
438         {
439           u_int32_t g = *glyph++;
440
441           for (cx = 0; cx < w; cx++)
442             {
443               if (g & 0x80000000)
444                 psplash_fb_plot_pixel (fb, x+dx+cx, y+dy+cy,
445                                        red, green, blue);
446               g <<= 1;
447             }
448         }
449
450       dx += w;
451     }
452 }
453