4 * Copyright (c) 1999 Michael Smith <msmith@freebsd.org>
5 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@freebsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $FreeBSD: src/sys/modules/splash/bmp/splash_bmp.c,v 1.10.2.3 2000/10/31 08:00:06 nyan Exp $
30 * $DragonFly: src/sys/dev/video/fb/bmp/splash_bmp.c,v 1.10 2007/09/15 13:18:40 swildner Exp $
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/linker.h>
38 #include <sys/thread.h>
40 #include <dev/video/fb/fbreg.h>
41 #include <dev/video/fb/splashreg.h>
42 #include <dev/video/fb/vgareg.h>
44 #include <bus/isa/isareg.h>
46 #define FADE_TIMEOUT 15 /* sec */
47 #define FADE_LEVELS 10
49 static int splash_mode = -1;
50 static int splash_on = FALSE;
52 static int bmp_start(video_adapter_t *);
53 static int bmp_end(video_adapter_t *);
54 static int bmp_splash(video_adapter_t *, int);
55 static int bmp_Init(const char *, int, int, int);
56 static int bmp_Draw(video_adapter_t *);
58 static splash_decoder_t bmp_decoder = {
59 "splash_bmp", bmp_start, bmp_end, bmp_splash, SPLASH_IMAGE,
62 SPLASH_DECODER(splash_bmp, bmp_decoder);
65 bmp_start(video_adapter_t *adp)
67 /* currently only 256-color modes are supported XXX */
68 static int modes[] = {
74 * As 320x200 doesn't generally look great,
75 * it's least preferred here.
83 lwkt_gettoken(&tty_token);
84 if ((bmp_decoder.data == NULL) || (bmp_decoder.data_size <= 0)) {
85 kprintf("splash_bmp: No bitmap file found\n");
86 lwkt_reltoken(&tty_token);
89 for (i = 0; modes[i] >= 0; ++i) {
90 if (((*vidsw[adp->va_index]->get_info)(adp, modes[i], &info) == 0)
91 && (bmp_Init((u_char *)bmp_decoder.data,
92 info.vi_width, info.vi_height, info.vi_depth) == 0))
95 splash_mode = modes[i];
97 kprintf("splash_bmp: No appropriate video mode found\n");
99 kprintf("bmp_start(): splash_mode:%d\n", splash_mode);
100 lwkt_reltoken(&tty_token);
101 return ((splash_mode < 0) ? ENODEV : 0);
105 bmp_end(video_adapter_t *adp)
112 bmp_splash(video_adapter_t *adp, int on)
114 static u_char pal[256*3];
115 static long time_stamp;
117 static int fading = TRUE, brightness = FADE_LEVELS;
121 lwkt_gettoken(&tty_token);
124 /* set up the video mode and draw something */
125 if ((*vidsw[adp->va_index]->set_mode)(adp, splash_mode)) {
126 lwkt_reltoken(&tty_token);
130 lwkt_reltoken(&tty_token);
133 (*vidsw[adp->va_index]->save_palette)(adp, pal);
138 * This is a kludge to fade the image away. This section of the
139 * code takes effect only after the system is completely up.
140 * FADE_TIMEOUT should be configurable.
145 time_stamp = tv.tv_sec;
146 if (tv.tv_sec > time_stamp + FADE_TIMEOUT) {
148 if (brightness == 0) {
154 if (brightness == FADE_LEVELS) {
159 for (i = 0; i < sizeof(pal); ++i) {
160 tpal[i] = pal[i] * brightness / FADE_LEVELS;
162 (*vidsw[adp->va_index]->load_palette)(adp, tpal);
163 time_stamp = tv.tv_sec;
166 lwkt_reltoken(&tty_token);
169 /* the video mode will be restored by the caller */
171 lwkt_reltoken(&tty_token);
177 ** Code to handle Microsoft DIB (".BMP") format images.
179 ** Blame me (msmith@freebsd.org) if this is broken, not Soren.
182 typedef struct tagBITMAPFILEHEADER { /* bmfh */
188 } __packed BITMAPFILEHEADER;
190 typedef struct tagBITMAPINFOHEADER { /* bmih */
202 } __packed BITMAPINFOHEADER;
204 typedef struct tagRGBQUAD { /* rgbq */
211 typedef struct tagBITMAPINFO { /* bmi */
212 BITMAPINFOHEADER bmiHeader;
213 RGBQUAD bmiColors[256];
214 } __packed BITMAPINFO;
216 typedef struct tagBITMAPF
218 BITMAPFILEHEADER bmfh;
227 ** all we actually care about the image
231 int width,height; /* image dimensions */
232 int swidth,sheight; /* screen dimensions for the current mode */
233 u_char depth; /* image depth (1, 4, 8, 24 bits) */
234 u_char sdepth; /* screen depth (1, 4, 8 bpp) */
235 int ncols; /* number of colours */
236 u_char palette[256][3]; /* raw palette data */
237 u_char format; /* one of the BI_* constants above */
238 const u_char *data; /* pointer to the raw data */
239 const u_char *index; /* running pointer to the data while drawing */
240 u_char *vidmem; /* video memory allocated for drawing */
241 video_adapter_t *adp;
245 static BMP_INFO bmp_info;
250 ** Given (info), set the pixel at (x),(y) to (val)
254 bmp_SetPix(BMP_INFO *info, int x, int y, u_char val)
260 * range check to avoid explosions
262 if ((x < 0) || (x >= info->swidth) || (y < 0) || (y >= info->sheight))
265 lwkt_gettoken(&tty_token);
267 * calculate offset into video memory;
268 * because 0,0 is bottom-left for DIB, we have to convert.
270 sofs = ((info->height - (y+1) + (info->sheight - info->height) / 2)
271 * info->adp->va_line_width);
272 x += (info->swidth - info->width) / 2;
274 switch(info->sdepth) {
277 /* EGA/VGA planar modes */
279 newbank = sofs/info->adp->va_window_size;
280 if (info->bank != newbank) {
281 (*vidsw[info->adp->va_index]->set_win_org)(info->adp, newbank*info->adp->va_window_size);
282 info->bank = newbank;
284 sofs %= info->adp->va_window_size;
285 bofs = x & 0x7; /* offset within byte */
286 outw(GDCIDX, (0x8000 >> bofs) | 0x08); /* bit mask */
287 outw(GDCIDX, (val << 8) | 0x00); /* set/reset */
288 *(info->vidmem + sofs) ^= 0xff; /* read-modify-write */
293 newbank = sofs/info->adp->va_window_size;
294 if (info->bank != newbank) {
295 (*vidsw[info->adp->va_index]->set_win_org)(info->adp, newbank*info->adp->va_window_size);
296 info->bank = newbank;
298 sofs %= info->adp->va_window_size;
299 *(info->vidmem+sofs) = val;
302 lwkt_reltoken(&tty_token);
308 ** Given (data) pointing to a line of RLE4-format data and (line) being the starting
309 ** line onscreen, decode the line.
312 bmp_DecodeRLE4(BMP_INFO *info, int line)
314 int count; /* run count */
316 int x,y; /* screen position */
318 x = 0; /* starting position */
321 /* loop reading data */
324 * encoded mode starts with a run length, and then a byte with
325 * two colour indexes to alternate between for the run
328 for (count = 0; count < *info->index; count++, x++) {
329 if (count & 1) { /* odd count, low nybble */
330 bmp_SetPix(info, x, y, *(info->index+1) & 0x0f);
331 } else { /* even count, high nybble */
332 bmp_SetPix(info, x, y, (*(info->index+1) >>4) & 0x0f);
337 * A leading zero is an escape; it may signal the end of the
338 * bitmap, a cursor move, or some absolute data.
340 } else { /* zero tag may be absolute mode or an escape */
341 switch (*(info->index+1)) {
342 case 0: /* end of line */
345 case 1: /* end of bitmap */
349 x += *(info->index + 2); /* new coords */
350 y += *(info->index + 3);
353 default: /* literal bitmap data */
354 for (count = 0; count < *(info->index + 1); count++, x++) {
355 val = *(info->index + 2 + (count / 2)); /* byte with nybbles */
357 val &= 0xf; /* get low nybble */
359 val = (val >> 4); /* get high nybble */
361 bmp_SetPix(info, x, y, val);
363 /* warning, this depends on integer truncation, do not hand-optimise! */
364 info->index += 2 + ((count + 3) / 4) * 2;
373 ** Given (data) pointing to a line of RLE8-format data and (line) being the starting
374 ** line onscreen, decode the line.
377 bmp_DecodeRLE8(BMP_INFO *info, int line)
379 int count; /* run count */
380 int x,y; /* screen position */
382 x = 0; /* starting position */
385 /* loop reading data */
388 * encoded mode starts with a run length, and then a byte with
389 * two colour indexes to alternate between for the run
392 for (count = 0; count < *info->index; count++, x++)
393 bmp_SetPix(info, x, y, *(info->index+1));
396 * A leading zero is an escape; it may signal the end of the
397 * bitmap, a cursor move, or some absolute data.
399 } else { /* zero tag may be absolute mode or an escape */
400 switch(*(info->index+1)) {
401 case 0: /* end of line */
404 case 1: /* end of bitmap */
408 x += *(info->index + 2); /* new coords */
409 y += *(info->index + 3);
412 default: /* literal bitmap data */
413 for (count = 0; count < *(info->index + 1); count++, x++)
414 bmp_SetPix(info, x, y, *(info->index + 2 + count));
415 /* must be an even count */
416 info->index += 2 + count + (count & 1);
426 ** Given (info) pointing to an image being decoded, (line) being the line currently
427 ** being displayed, decode a line of data.
430 bmp_DecodeLine(BMP_INFO *info, int line)
436 switch(info->format) {
438 switch(info->depth) {
440 for (x = 0; x < info->width; x++, info->index++)
441 bmp_SetPix(info, x, line, *info->index);
442 info->index += 3 - (--x % 4);
446 for (x = 0; x < info->width; x++) {
448 val = *p & 0xf; /* get low nybble */
451 val = *p >> 4; /* get high nybble */
453 bmp_SetPix(info, x, line, val);
455 /* warning, this depends on integer truncation, do not hand-optimise! */
456 info->index += ((x + 7) / 8) * 4;
461 for (x = 0; x < info->width; x++) {
462 val = (*p & mask) ? 1 : 0;
468 bmp_SetPix(info, x, line, val);
470 /* warning, this depends on integer truncation, do not hand-optimise! */
471 info->index += ((x + 31) / 32) * 4;
476 bmp_DecodeRLE4(info, line);
479 bmp_DecodeRLE8(info, line);
487 ** Given a pointer (data) to the image of a BMP file, fill in bmp_info with what
488 ** can be learnt from it. Return nonzero if the file isn't usable.
490 ** Take screen dimensions (swidth), (sheight) and (sdepth) and make sure we
491 ** can work with these.
494 bmp_Init(const char *data, int swidth, int sheight, int sdepth)
496 const BITMAPF *bmf = (const BITMAPF *)data;
499 bmp_info.data = NULL; /* assume setup failed */
502 if (bmf->bmfh.bfType != 0x4d42) {
503 kprintf("splash_bmp: not a BMP file\n");
504 return(1); /* XXX check word ordering for big-endian ports? */
507 /* do we understand this bitmap format? */
508 if (bmf->bmfi.bmiHeader.biSize > sizeof(bmf->bmfi.bmiHeader)) {
509 kprintf("splash_bmp: unsupported BMP format (size=%d)\n",
510 bmf->bmfi.bmiHeader.biSize);
514 /* save what we know about the screen */
515 bmp_info.swidth = swidth;
516 bmp_info.sheight = sheight;
517 bmp_info.sdepth = sdepth;
519 /* where's the data? */
520 bmp_info.data = (const u_char *)data + bmf->bmfh.bfOffBits;
522 /* image parameters */
523 bmp_info.width = bmf->bmfi.bmiHeader.biWidth;
524 bmp_info.height = bmf->bmfi.bmiHeader.biHeight;
525 bmp_info.depth = bmf->bmfi.bmiHeader.biBitCount;
526 bmp_info.format = bmf->bmfi.bmiHeader.biCompression;
528 switch(bmp_info.format) { /* check compression format */
534 kprintf("splash_bmp: unsupported compression format\n");
535 return(1); /* unsupported compression format */
538 /* palette details */
539 bmp_info.ncols = (bmf->bmfi.bmiHeader.biClrUsed);
540 bzero(bmp_info.palette,sizeof(bmp_info.palette));
541 if (bmp_info.ncols == 0) { /* uses all of them */
542 bmp_info.ncols = 1 << bmf->bmfi.bmiHeader.biBitCount;
544 if ((bmp_info.height > bmp_info.sheight) ||
545 (bmp_info.width > bmp_info.swidth) ||
546 (bmp_info.ncols > (1 << sdepth))) {
548 kprintf("splash_bmp: beyond screen capacity (%dx%d, %d colors)\n",
549 bmp_info.width, bmp_info.height, bmp_info.ncols);
554 for (pind = 0; pind < bmp_info.ncols; pind++) {
555 bmp_info.palette[pind][0] = bmf->bmfi.bmiColors[pind].rgbRed;
556 bmp_info.palette[pind][1] = bmf->bmfi.bmiColors[pind].rgbGreen;
557 bmp_info.palette[pind][2] = bmf->bmfi.bmiColors[pind].rgbBlue;
565 ** Render the image. Return nonzero if that's not possible.
569 bmp_Draw(video_adapter_t *adp)
576 if (bmp_info.data == NULL) { /* init failed, do nothing */
580 lwkt_gettoken(&tty_token);
581 /* clear the screen */
582 bmp_info.vidmem = (u_char *)adp->va_window;
584 (*vidsw[adp->va_index]->clear)(adp);
585 (*vidsw[adp->va_index]->set_win_org)(adp, 0);
588 /* initialise the info structure for drawing */
589 bmp_info.index = bmp_info.data;
591 /* set the palette for our image */
592 (*vidsw[adp->va_index]->load_palette)(adp, (u_char *)&bmp_info.palette);
595 /* XXX: this is ugly, but necessary for EGA/VGA 1bpp/4bpp modes */
596 if ((adp->va_type == KD_EGA) || (adp->va_type == KD_VGA)) {
597 inb(adp->va_crtc_addr + 6); /* reset flip-flop */
600 for (i = 0; i < 16; ++i) {
604 inb(adp->va_crtc_addr + 6); /* reset flip-flop */
605 outb(ATC, 0x20); /* enable palette */
607 outw(GDCIDX, 0x0f01); /* set/reset enable */
609 if (bmp_info.sdepth == 1)
610 outw(TSIDX, 0x0102); /* unmask plane #0 */
614 for (line = 0; (line < bmp_info.height) && bmp_info.index; line++) {
615 bmp_DecodeLine(&bmp_info, line);
617 lwkt_reltoken(&tty_token);