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 psplash_fb_new (int angle)
31 struct fb_var_screeninfo fb_var;
32 struct fb_fix_screeninfo fb_fix;
38 fbdev = getenv("FBDEV");
42 if ((fb = malloc (sizeof(PSplashFB))) == NULL)
44 perror ("Error no memory");
48 memset (fb, 0, sizeof(PSplashFB));
52 if ((fb->fd = open (fbdev, O_RDWR)) < 0)
54 perror ("Error opening /dev/fb0");
58 if (ioctl (fb->fd, FBIOGET_FSCREENINFO, &fb_fix) == -1
59 || ioctl (fb->fd, FBIOGET_VSCREENINFO, &fb_var) == -1)
61 perror ("Error getting framebuffer info");
65 if (fb_var.bits_per_pixel < 16)
68 "Error, no support currently for %i bpp frame buffers\n",
69 fb_var.bits_per_pixel);
72 fb->real_width = fb->width = fb_var.xres;
73 fb->real_height = fb->height = fb_var.yres;
74 fb->bpp = fb_var.bits_per_pixel;
75 fb->stride = fb_fix.line_length;
76 fb->type = fb_fix.type;
77 fb->visual = fb_fix.visual;
79 DBG("width: %i, height: %i, bpp: %i, stride: %i",
80 fb->width, fb->height, fb->bpp, fb->stride);
82 fb->base = (char *) mmap ((caddr_t) NULL,
84 fb->stride * fb->height,
89 if (fb->base == (char *)-1)
91 perror("Error cannot mmap framebuffer ");
95 off = (unsigned long) fb_fix.smem_start % (unsigned long) getpagesize();
97 fb->data = fb->base + off;
100 /* FIXME: No support for 8pp as yet */
101 if (visual == FB_VISUAL_PSEUDOCOLOR
102 || visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
104 static struct fb_cmap cmap;
108 cmap.red = saved_red;
109 cmap.green = saved_green;
110 cmap.blue = saved_blue;
113 ioctl (fb, FBIOGETCMAP, &cmap);
127 fb->width = fb->real_height;
128 fb->height = fb->real_width;
141 psplash_fb_destroy (fb);
146 #define OFFSET(fb,x,y) (((y) * (fb)->stride) + ((x) * ((fb)->bpp >> 3)))
149 psplash_fb_plot_pixel (PSplashFB *fb,
158 if (x < 0 || x > fb->width-1 || y < 0 || y > fb->height-1)
164 off = OFFSET (fb, fb->height - y - 1, x);
167 off = OFFSET (fb, fb->width - x - 1, fb->height - y - 1);
170 off = OFFSET (fb, y, fb->width - x - 1);
174 off = OFFSET (fb, x, y);
178 /* FIXME: handle no RGB orderings */
183 *(fb->data + off) = red;
184 *(fb->data + off + 1) = green;
185 *(fb->data + off + 2) = blue;
188 *(volatile uint16 *) (fb->data + off)
189 = ((red >> 3) << 11) | ((green >> 2) << 5) | (blue >> 3);
192 /* depth not supported yet */
199 psplash_fb_draw_rect (PSplashFB *fb,
210 for (dy=0; dy < height; dy++)
211 for (dx=0; dx < width; dx++)
212 psplash_fb_plot_pixel (fb, x+dx, y+dy, red, green, blue);
216 psplash_fb_draw_image (PSplashFB *fb,
221 int img_bytes_per_pixel,
225 int dx = 0, dy = 0, total_len;
228 total_len = img_width * img_height * img_bytes_per_pixel;
230 /* FIXME: Optimise, check for over runs ... */
231 while ((p - rle_data) < total_len)
243 psplash_fb_plot_pixel (fb, x+dx, y+dy, *p, *(p+1), *(p+2));
244 if (++dx >= img_width) { dx=0; dy++; }
246 while (--len && (p - rle_data) < total_len);
248 p += img_bytes_per_pixel;
256 psplash_fb_plot_pixel (fb, x+dx, y+dy, *p, *(p+1), *(p+2));
257 if (++dx >= img_width) { dx=0; dy++; }
258 p += img_bytes_per_pixel;
260 while (--len && (p - rle_data) < total_len);
265 /* Font rendering code based on BOGL by Ben Pfaff */
268 psplash_font_glyph (const PSplashFont *font, wchar_t wc, u_int32_t **bitmap)
270 int mask = font->index_mask;
275 for (i = font->offset[wc & mask]; font->index[i]; i += 2)
277 if ((font->index[i] & ~mask) == (wc & ~mask))
280 *bitmap = &font->content[font->index[i+1]];
281 return font->index[i] & mask;
289 psplash_fb_text_size (PSplashFB *fb,
292 const PSplashFont *font,
295 char *c = (char*)text;
303 for (; (k = mbtowc (&wc, c, n)) > 0; c += k, n -= k)
313 w += psplash_font_glyph (font, wc, NULL);
316 *width = (w > mw) ? w : mw;
317 *height = (h == 0) ? font->height : h;
321 psplash_fb_draw_text (PSplashFB *fb,
327 const PSplashFont *font,
330 int h, w, k, n, cx, cy, dx, dy;
331 char *c = (char*)text;
339 for (; (k = mbtowc (&wc, c, n)) > 0; c += k, n -= k)
341 u_int32_t *glyph = NULL;
350 w = psplash_font_glyph (font, wc, &glyph);
355 for (cy = 0; cy < h; cy++)
357 u_int32_t g = *glyph++;
359 for (cx = 0; cx < w; cx++)
362 psplash_fb_plot_pixel (fb, x+dx+cx, y+dy+cy,