2 * Copyright (c) 1999 Michael Smith <msmith@freebsd.org>
3 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/linker.h>
37 #include <dev/fb/fbreg.h>
38 #include <dev/fb/splashreg.h>
40 #include <dev/fb/vgareg.h>
42 #include <isa/isareg.h>
45 #define FADE_TIMEOUT 15 /* sec */
46 #define FADE_LEVELS 10
48 static int splash_mode = -1;
49 static int splash_on = FALSE;
51 static int bmp_start(video_adapter_t *adp);
52 static int bmp_end(video_adapter_t *adp);
53 static int bmp_splash(video_adapter_t *adp, int on);
54 static int bmp_Init(char *data, int swidth, int sheight, int sdepth);
55 static int bmp_Draw(video_adapter_t *adp);
57 static splash_decoder_t bmp_decoder = {
58 "splash_bmp", bmp_start, bmp_end, bmp_splash, SPLASH_IMAGE,
61 SPLASH_DECODER(splash_bmp, bmp_decoder);
64 bmp_start(video_adapter_t *adp)
66 /* currently only 256-color modes are supported XXX */
67 static int modes[] = {
70 * As 640x400 doesn't generally look great,
71 * it's least preferred here.
82 * As 320x200 doesn't generally look great,
83 * it's least preferred here.
92 if ((bmp_decoder.data == NULL) || (bmp_decoder.data_size <= 0)) {
93 printf("splash_bmp: No bitmap file found\n");
96 for (i = 0; modes[i] >= 0; ++i) {
97 if ((vidd_get_info(adp, modes[i], &info) == 0) &&
98 (bmp_Init((u_char *)bmp_decoder.data, info.vi_width,
99 info.vi_height, info.vi_depth) == 0))
102 splash_mode = modes[i];
104 printf("splash_bmp: No appropriate video mode found\n");
106 printf("bmp_start(): splash_mode:%d\n", splash_mode);
107 return ((splash_mode < 0) ? ENODEV : 0);
111 bmp_end(video_adapter_t *adp)
118 bmp_splash(video_adapter_t *adp, int on)
120 static u_char pal[256*3];
121 static long time_stamp;
123 static int fading = TRUE, brightness = FADE_LEVELS;
129 /* set up the video mode and draw something */
130 if (vidd_set_mode(adp, splash_mode))
134 vidd_save_palette(adp, pal);
139 * This is a kludge to fade the image away. This section of the
140 * code takes effect only after the system is completely up.
141 * FADE_TIMEOUT should be configurable.
146 time_stamp = tv.tv_sec;
147 if (tv.tv_sec > time_stamp + FADE_TIMEOUT) {
149 if (brightness == 0) {
155 if (brightness == FADE_LEVELS) {
160 for (i = 0; i < sizeof(pal); ++i) {
161 tpal[i] = pal[i] * brightness / FADE_LEVELS;
163 vidd_load_palette(adp, tpal);
164 time_stamp = tv.tv_sec;
169 /* the video mode will be restored by the caller */
176 ** Code to handle Microsoft DIB (".BMP") format images.
178 ** Blame me (msmith@freebsd.org) if this is broken, not Soren.
181 typedef struct tagBITMAPFILEHEADER { /* bmfh */
187 } __packed BITMAPFILEHEADER;
189 typedef struct tagBITMAPINFOHEADER { /* bmih */
201 } __packed BITMAPINFOHEADER;
203 typedef struct tagRGBQUAD { /* rgbq */
210 typedef struct tagBITMAPINFO { /* bmi */
211 BITMAPINFOHEADER bmiHeader;
212 RGBQUAD bmiColors[256];
213 } __packed BITMAPINFO;
215 typedef struct tagBITMAPF
217 BITMAPFILEHEADER bmfh;
226 ** all we actually care about the image
230 int width,height; /* image dimensions */
231 int swidth,sheight; /* screen dimensions for the current mode */
232 u_char depth; /* image depth (1, 4, 8, 24 bits) */
233 u_char sdepth; /* screen depth (1, 4, 8 bpp) */
234 int ncols; /* number of colours */
235 u_char palette[256][3]; /* raw palette data */
236 u_char format; /* one of the BI_* constants above */
237 u_char *data; /* pointer to the raw data */
238 u_char *index; /* running pointer to the data while drawing */
239 u_char *vidmem; /* video memory allocated for drawing */
240 video_adapter_t *adp;
247 static BMP_INFO bmp_info;
252 ** Given (info), set the pixel at (x),(y) to (val)
256 bmp_SetPix(BMP_INFO *info, int x, int y, u_char val)
262 * range check to avoid explosions
264 if ((x < 0) || (x >= info->swidth) || (y < 0) || (y >= info->sheight))
268 * calculate offset into video memory;
269 * because 0,0 is bottom-left for DIB, we have to convert.
271 sofs = ((info->height - (y+1) + (info->sheight - info->height) / 2)
272 * info->adp->va_line_width);
273 x += (info->swidth - info->width) / 2;
275 switch(info->sdepth) {
279 bofs = x & 0x7; /* offset within byte */
281 outb(0x7c, 0x80 | 0x40); /* GRCG on & RMW mode */
282 if (val != info->prev_val) {
283 outb(0x7e, (val & 1) ? 0xff : 0); /* tile B */
284 outb(0x7e, (val & 2) ? 0xff : 0); /* tile R */
285 outb(0x7e, (val & 4) ? 0xff : 0); /* tile G */
286 outb(0x7e, (val & 8) ? 0xff : 0); /* tile I */
288 info->prev_val = val;
291 *(info->vidmem+sofs) = (0x80 >> bofs); /* write new bit */
292 outb(0x7c, 0); /* GRCG off */
297 /* EGA/VGA planar modes */
299 newbank = sofs/info->adp->va_window_size;
300 if (info->bank != newbank) {
301 vidd_set_win_org(info->adp, newbank*info->adp->va_window_size);
302 info->bank = newbank;
304 sofs %= info->adp->va_window_size;
305 bofs = x & 0x7; /* offset within byte */
306 outw(GDCIDX, (0x8000 >> bofs) | 0x08); /* bit mask */
307 outw(GDCIDX, (val << 8) | 0x00); /* set/reset */
308 *(info->vidmem + sofs) ^= 0xff; /* read-modify-write */
314 newbank = sofs/info->adp->va_window_size;
315 if (info->bank != newbank) {
316 vidd_set_win_org(info->adp, newbank*info->adp->va_window_size);
317 info->bank = newbank;
319 sofs %= info->adp->va_window_size;
320 *(info->vidmem+sofs) = val;
328 ** Given (data) pointing to a line of RLE4-format data and (line) being the starting
329 ** line onscreen, decode the line.
332 bmp_DecodeRLE4(BMP_INFO *info, int line)
334 int count; /* run count */
336 int x,y; /* screen position */
338 x = 0; /* starting position */
341 /* loop reading data */
344 * encoded mode starts with a run length, and then a byte with
345 * two colour indexes to alternate between for the run
348 for (count = 0; count < *info->index; count++, x++) {
349 if (count & 1) { /* odd count, low nybble */
350 bmp_SetPix(info, x, y, *(info->index+1) & 0x0f);
351 } else { /* even count, high nybble */
352 bmp_SetPix(info, x, y, (*(info->index+1) >>4) & 0x0f);
357 * A leading zero is an escape; it may signal the end of the
358 * bitmap, a cursor move, or some absolute data.
360 } else { /* zero tag may be absolute mode or an escape */
361 switch (*(info->index+1)) {
362 case 0: /* end of line */
365 case 1: /* end of bitmap */
369 x += *(info->index + 2); /* new coords */
370 y += *(info->index + 3);
373 default: /* literal bitmap data */
374 for (count = 0; count < *(info->index + 1); count++, x++) {
375 val = *(info->index + 2 + (count / 2)); /* byte with nybbles */
377 val &= 0xf; /* get low nybble */
379 val = (val >> 4); /* get high nybble */
381 bmp_SetPix(info, x, y, val);
383 /* warning, this depends on integer truncation, do not hand-optimise! */
384 info->index += 2 + ((count + 3) / 4) * 2;
393 ** Given (data) pointing to a line of RLE8-format data and (line) being the starting
394 ** line onscreen, decode the line.
397 bmp_DecodeRLE8(BMP_INFO *info, int line)
399 int count; /* run count */
400 int x,y; /* screen position */
402 x = 0; /* starting position */
405 /* loop reading data */
408 * encoded mode starts with a run length, and then a byte with
409 * two colour indexes to alternate between for the run
412 for (count = 0; count < *info->index; count++, x++)
413 bmp_SetPix(info, x, y, *(info->index+1));
416 * A leading zero is an escape; it may signal the end of the
417 * bitmap, a cursor move, or some absolute data.
419 } else { /* zero tag may be absolute mode or an escape */
420 switch(*(info->index+1)) {
421 case 0: /* end of line */
424 case 1: /* end of bitmap */
428 x += *(info->index + 2); /* new coords */
429 y += *(info->index + 3);
432 default: /* literal bitmap data */
433 for (count = 0; count < *(info->index + 1); count++, x++)
434 bmp_SetPix(info, x, y, *(info->index + 2 + count));
435 /* must be an even count */
436 info->index += 2 + count + (count & 1);
446 ** Given (info) pointing to an image being decoded, (line) being the line currently
447 ** being displayed, decode a line of data.
450 bmp_DecodeLine(BMP_INFO *info, int line)
453 u_char val, mask, *p;
455 switch(info->format) {
457 switch(info->depth) {
459 for (x = 0; x < info->width; x++, info->index++)
460 bmp_SetPix(info, x, line, *info->index);
461 info->index += 3 - (--x % 4);
465 for (x = 0; x < info->width; x++) {
467 val = *p & 0xf; /* get low nybble */
470 val = *p >> 4; /* get high nybble */
472 bmp_SetPix(info, x, line, val);
474 /* warning, this depends on integer truncation, do not hand-optimise! */
475 info->index += ((x + 7) / 8) * 4;
480 for (x = 0; x < info->width; x++) {
481 val = (*p & mask) ? 1 : 0;
487 bmp_SetPix(info, x, line, val);
489 /* warning, this depends on integer truncation, do not hand-optimise! */
490 info->index += ((x + 31) / 32) * 4;
495 bmp_DecodeRLE4(info, line);
498 bmp_DecodeRLE8(info, line);
506 ** Given a pointer (data) to the image of a BMP file, fill in bmp_info with what
507 ** can be learnt from it. Return nonzero if the file isn't usable.
509 ** Take screen dimensions (swidth), (sheight) and (sdepth) and make sure we
510 ** can work with these.
513 bmp_Init(char *data, int swidth, int sheight, int sdepth)
515 BITMAPF *bmf = (BITMAPF *)data;
518 bmp_info.data = NULL; /* assume setup failed */
521 if (bmf->bmfh.bfType != 0x4d42) {
522 printf("splash_bmp: not a BMP file\n");
523 return(1); /* XXX check word ordering for big-endian ports? */
526 /* do we understand this bitmap format? */
527 if (bmf->bmfi.bmiHeader.biSize > sizeof(bmf->bmfi.bmiHeader)) {
528 printf("splash_bmp: unsupported BMP format (size=%d)\n",
529 bmf->bmfi.bmiHeader.biSize);
533 /* save what we know about the screen */
534 bmp_info.swidth = swidth;
535 bmp_info.sheight = sheight;
536 bmp_info.sdepth = sdepth;
538 /* where's the data? */
539 bmp_info.data = (u_char *)data + bmf->bmfh.bfOffBits;
541 /* image parameters */
542 bmp_info.width = bmf->bmfi.bmiHeader.biWidth;
543 bmp_info.height = bmf->bmfi.bmiHeader.biHeight;
544 bmp_info.depth = bmf->bmfi.bmiHeader.biBitCount;
545 bmp_info.format = bmf->bmfi.bmiHeader.biCompression;
547 switch(bmp_info.format) { /* check compression format */
553 printf("splash_bmp: unsupported compression format\n");
554 return(1); /* unsupported compression format */
557 /* palette details */
558 bmp_info.ncols = (bmf->bmfi.bmiHeader.biClrUsed);
559 bzero(bmp_info.palette,sizeof(bmp_info.palette));
560 if (bmp_info.ncols == 0) { /* uses all of them */
561 bmp_info.ncols = 1 << bmf->bmfi.bmiHeader.biBitCount;
563 if ((bmp_info.height > bmp_info.sheight) ||
564 (bmp_info.width > bmp_info.swidth) ||
565 (bmp_info.ncols > (1 << sdepth))) {
567 printf("splash_bmp: beyond screen capacity (%dx%d, %d colors)\n",
568 bmp_info.width, bmp_info.height, bmp_info.ncols);
573 for (pind = 0; pind < bmp_info.ncols; pind++) {
574 bmp_info.palette[pind][0] = bmf->bmfi.bmiColors[pind].rgbRed;
575 bmp_info.palette[pind][1] = bmf->bmfi.bmiColors[pind].rgbGreen;
576 bmp_info.palette[pind][2] = bmf->bmfi.bmiColors[pind].rgbBlue;
584 ** Render the image. Return nonzero if that's not possible.
588 bmp_Draw(video_adapter_t *adp)
597 if (bmp_info.data == NULL) { /* init failed, do nothing */
601 /* clear the screen */
602 bmp_info.vidmem = (u_char *)adp->va_window;
605 vidd_set_win_org(adp, 0);
608 /* initialise the info structure for drawing */
609 bmp_info.index = bmp_info.data;
611 bmp_info.prev_val = 255;
614 /* set the palette for our image */
615 vidd_load_palette(adp, (u_char *)&bmp_info.palette);
619 /* XXX: this is ugly, but necessary for EGA/VGA 1bpp/4bpp modes */
620 if ((adp->va_type == KD_EGA) || (adp->va_type == KD_VGA)) {
621 inb(adp->va_crtc_addr + 6); /* reset flip-flop */
624 for (i = 0; i < 16; ++i) {
628 inb(adp->va_crtc_addr + 6); /* reset flip-flop */
629 outb(ATC, 0x20); /* enable palette */
631 outw(GDCIDX, 0x0f01); /* set/reset enable */
633 if (bmp_info.sdepth == 1)
634 outw(TSIDX, 0x0102); /* unmask plane #0 */
639 for (line = 0; (line < bmp_info.height) && bmp_info.index; line++) {
640 bmp_DecodeLine(&bmp_info, line);