2 * pslash - a lightweight framebuffer splashscreen for embedded devices.
4 * Copyright (c) 2006 Matthew Allum <mallum@o-hand.com>
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)
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.
20 psplash_fb_destroy (PSplashFB *fb)
29 attempt_to_change_pixel_format (PSplashFB *fb,
30 struct fb_var_screeninfo *fb_var)
32 /* By default the framebuffer driver may have set an oversized
33 * yres_virtual to support VT scrolling via the panning interface.
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.
39 fb_var->yres_virtual = fb_var->yres;
41 /* First try setting an 8,8,8,0 pixel format so we don't have to do
42 * any conversions while drawing. */
44 fb_var->bits_per_pixel = 32;
46 fb_var->red.offset = 0;
47 fb_var->red.length = 8;
49 fb_var->green.offset = 8;
50 fb_var->green.length = 8;
52 fb_var->blue.offset = 16;
53 fb_var->blue.length = 8;
55 fb_var->transp.offset = 0;
56 fb_var->transp.length = 0;
58 if (ioctl (fb->fd, FBIOPUT_VSCREENINFO, fb_var) == 0)
60 fprintf(stdout, "Switched to a 32 bpp 8,8,8 frame buffer\n");
66 "Error, failed to switch to a 32 bpp 8,8,8 frame buffer\n");
69 /* Otherwise try a 16bpp 5,6,5 format */
71 fb_var->bits_per_pixel = 16;
73 fb_var->red.offset = 11;
74 fb_var->red.length = 5;
76 fb_var->green.offset = 5;
77 fb_var->green.length = 6;
79 fb_var->blue.offset = 0;
80 fb_var->blue.length = 5;
82 fb_var->transp.offset = 0;
83 fb_var->transp.length = 0;
85 if (ioctl (fb->fd, FBIOPUT_VSCREENINFO, fb_var) == 0)
87 fprintf(stdout, "Switched to a 16 bpp 5,6,5 frame buffer\n");
93 "Error, failed to switch to a 16 bpp 5,6,5 frame buffer\n");
100 psplash_fb_new (int angle)
102 struct fb_var_screeninfo fb_var;
103 struct fb_fix_screeninfo fb_fix;
107 PSplashFB *fb = NULL;
109 fbdev = getenv("FBDEV");
113 if ((fb = malloc (sizeof(PSplashFB))) == NULL)
115 perror ("Error no memory");
119 memset (fb, 0, sizeof(PSplashFB));
123 if ((fb->fd = open (fbdev, O_RDWR)) < 0)
125 perror ("Error opening /dev/fb0");
129 if (ioctl (fb->fd, FBIOGET_VSCREENINFO, &fb_var) == -1)
131 perror ("Error getting variable framebuffer info");
135 if (fb_var.bits_per_pixel < 16)
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))
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)
150 perror ("Error getting fixed framebuffer info");
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;
161 DBG("width: %i, height: %i, bpp: %i, stride: %i",
162 fb->width, fb->height, fb->bpp, fb->stride);
164 fb->base = (char *) mmap ((caddr_t) NULL,
166 fb->stride * fb->height,
167 PROT_READ|PROT_WRITE,
171 if (fb->base == (char *)-1)
173 perror("Error cannot mmap framebuffer ");
177 off = (unsigned long) fb_fix.smem_start % (unsigned long) getpagesize();
179 fb->data = fb->base + off;
182 /* FIXME: No support for 8pp as yet */
183 if (visual == FB_VISUAL_PSEUDOCOLOR
184 || visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
186 static struct fb_cmap cmap;
190 cmap.red = saved_red;
191 cmap.green = saved_green;
192 cmap.blue = saved_blue;
195 ioctl (fb, FBIOGETCMAP, &cmap);
209 fb->width = fb->real_height;
210 fb->height = fb->real_width;
223 psplash_fb_destroy (fb);
228 #define OFFSET(fb,x,y) (((y) * (fb)->stride) + ((x) * ((fb)->bpp >> 3)))
231 psplash_fb_plot_pixel (PSplashFB *fb,
240 if (x < 0 || x > fb->width-1 || y < 0 || y > fb->height-1)
246 off = OFFSET (fb, fb->height - y - 1, x);
249 off = OFFSET (fb, fb->width - x - 1, fb->height - y - 1);
252 off = OFFSET (fb, y, fb->width - x - 1);
256 off = OFFSET (fb, x, y);
260 /* FIXME: handle no RGB orderings */
265 *(fb->data + off) = red;
266 *(fb->data + off + 1) = green;
267 *(fb->data + off + 2) = blue;
270 *(volatile uint16 *) (fb->data + off)
271 = ((red >> 3) << 11) | ((green >> 2) << 5) | (blue >> 3);
274 /* depth not supported yet */
281 psplash_fb_draw_rect (PSplashFB *fb,
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);
298 psplash_fb_draw_image (PSplashFB *fb,
303 int img_bytes_per_pixel,
307 int dx = 0, dy = 0, total_len;
310 total_len = img_width * img_height * img_bytes_per_pixel;
312 /* FIXME: Optimise, check for over runs ... */
313 while ((p - rle_data) < total_len)
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++; }
329 while (--len && (p - rle_data) < total_len);
331 p += img_bytes_per_pixel;
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;
344 while (--len && (p - rle_data) < total_len);
349 /* Font rendering code based on BOGL by Ben Pfaff */
352 psplash_font_glyph (const PSplashFont *font, wchar_t wc, u_int32_t **bitmap)
354 int mask = font->index_mask;
359 for (i = font->offset[wc & mask]; font->index[i]; i += 2)
361 if ((font->index[i] & ~mask) == (wc & ~mask))
364 *bitmap = &font->content[font->index[i+1]];
365 return font->index[i] & mask;
373 psplash_fb_text_size (PSplashFB *fb,
376 const PSplashFont *font,
379 char *c = (char*)text;
387 for (; (k = mbtowc (&wc, c, n)) > 0; c += k, n -= k)
397 w += psplash_font_glyph (font, wc, NULL);
400 *width = (w > mw) ? w : mw;
401 *height = (h == 0) ? font->height : h;
405 psplash_fb_draw_text (PSplashFB *fb,
411 const PSplashFont *font,
414 int h, w, k, n, cx, cy, dx, dy;
415 char *c = (char*)text;
423 for (; (k = mbtowc (&wc, c, n)) > 0; c += k, n -= k)
425 u_int32_t *glyph = NULL;
434 w = psplash_font_glyph (font, wc, &glyph);
439 for (cy = 0; cy < h; cy++)
441 u_int32_t g = *glyph++;
443 for (cx = 0; cx < w; cx++)
446 psplash_fb_plot_pixel (fb, x+dx+cx, y+dy+cy,