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/linker.h>
36 #include <dev/fb/fbreg.h>
37 #include <dev/fb/splashreg.h>
39 #include <dev/fb/vgareg.h>
41 #include <isa/isareg.h>
44 #define FADE_TIMEOUT 15 /* sec */
45 #define FADE_LEVELS 10
47 static int splash_mode = -1;
48 static int splash_on = FALSE;
50 static int bmp_start(video_adapter_t *adp);
51 static int bmp_end(video_adapter_t *adp);
52 static int bmp_splash(video_adapter_t *adp, int on);
53 static int bmp_Init(const char *data, int swidth, int sheight, int sdepth);
54 static int bmp_Draw(video_adapter_t *adp);
56 static splash_decoder_t bmp_decoder = {
57 "splash_bmp", bmp_start, bmp_end, bmp_splash, SPLASH_IMAGE,
60 SPLASH_DECODER(splash_bmp, bmp_decoder);
63 bmp_start(video_adapter_t *adp)
65 /* currently only 256-color modes are supported XXX */
66 static int modes[] = {
69 * As 640x400 doesn't generally look great,
70 * it's least preferred here.
81 * As 320x200 doesn't generally look great,
82 * it's least preferred here.
91 if ((bmp_decoder.data == NULL) || (bmp_decoder.data_size <= 0)) {
92 printf("splash_bmp: No bitmap file found\n");
95 for (i = 0; modes[i] >= 0; ++i) {
96 if (((*vidsw[adp->va_index]->get_info)(adp, modes[i], &info) == 0)
97 && (bmp_Init((u_char *)bmp_decoder.data,
98 info.vi_width, info.vi_height, info.vi_depth) == 0))
101 splash_mode = modes[i];
103 printf("splash_bmp: No appropriate video mode found\n");
105 printf("bmp_start(): splash_mode:%d\n", splash_mode);
106 return ((splash_mode < 0) ? ENODEV : 0);
110 bmp_end(video_adapter_t *adp)
117 bmp_splash(video_adapter_t *adp, int on)
119 static u_char pal[256*3];
120 static long time_stamp;
122 static int fading = TRUE, brightness = FADE_LEVELS;
128 /* set up the video mode and draw something */
129 if ((*vidsw[adp->va_index]->set_mode)(adp, splash_mode))
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;
168 /* the video mode will be restored by the caller */
175 ** Code to handle Microsoft DIB (".BMP") format images.
177 ** Blame me (msmith@freebsd.org) if this is broken, not Soren.
180 typedef struct tagBITMAPFILEHEADER { /* bmfh */
181 u_short bfType __attribute__ ((packed));
182 int bfSize __attribute__ ((packed));
183 u_short bfReserved1 __attribute__ ((packed));
184 u_short bfReserved2 __attribute__ ((packed));
185 int bfOffBits __attribute__ ((packed));
188 typedef struct tagBITMAPINFOHEADER { /* bmih */
189 int biSize __attribute__ ((packed));
190 int biWidth __attribute__ ((packed));
191 int biHeight __attribute__ ((packed));
192 short biPlanes __attribute__ ((packed));
193 short biBitCount __attribute__ ((packed));
194 int biCompression __attribute__ ((packed));
195 int biSizeImage __attribute__ ((packed));
196 int biXPelsPerMeter __attribute__ ((packed));
197 int biYPelsPerMeter __attribute__ ((packed));
198 int biClrUsed __attribute__ ((packed));
199 int biClrImportant __attribute__ ((packed));
202 typedef struct tagRGBQUAD { /* rgbq */
203 u_char rgbBlue __attribute__ ((packed));
204 u_char rgbGreen __attribute__ ((packed));
205 u_char rgbRed __attribute__ ((packed));
206 u_char rgbReserved __attribute__ ((packed));
209 typedef struct tagBITMAPINFO { /* bmi */
210 BITMAPINFOHEADER bmiHeader __attribute__ ((packed));
211 RGBQUAD bmiColors[256] __attribute__ ((packed));
214 typedef struct tagBITMAPF
216 BITMAPFILEHEADER bmfh __attribute__ ((packed));
217 BITMAPINFO bmfi __attribute__ ((packed));
225 ** all we actually care about the image
229 int width,height; /* image dimensions */
230 int swidth,sheight; /* screen dimensions for the current mode */
231 u_char depth; /* image depth (1, 4, 8, 24 bits) */
232 u_char sdepth; /* screen depth (1, 4, 8 bpp) */
233 int ncols; /* number of colours */
234 u_char palette[256][3]; /* raw palette data */
235 u_char format; /* one of the BI_* constants above */
236 u_char *data; /* pointer to the raw data */
237 u_char *index; /* running pointer to the data while drawing */
238 u_char *vidmem; /* video memory allocated for drawing */
239 video_adapter_t *adp;
246 static BMP_INFO bmp_info;
251 ** Given (info), set the pixel at (x),(y) to (val)
255 bmp_SetPix(BMP_INFO *info, int x, int y, u_char val)
261 * range check to avoid explosions
263 if ((x < 0) || (x >= info->swidth) || (y < 0) || (y >= info->sheight))
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) {
278 bofs = x & 0x7; /* offset within byte */
280 outb(0x7c, 0x80 | 0x40); /* GRCG on & RMW mode */
281 if (val != info->prev_val) {
282 outb(0x7e, (val & 1) ? 0xff : 0); /* tile B */
283 outb(0x7e, (val & 2) ? 0xff : 0); /* tile R */
284 outb(0x7e, (val & 4) ? 0xff : 0); /* tile G */
285 outb(0x7e, (val & 8) ? 0xff : 0); /* tile I */
287 info->prev_val = val;
290 *(info->vidmem+sofs) = (0x80 >> bofs); /* write new bit */
291 outb(0x7c, 0); /* GRCG off */
296 /* EGA/VGA planar modes */
298 newbank = sofs/info->adp->va_window_size;
299 if (info->bank != newbank) {
300 (*vidsw[info->adp->va_index]->set_win_org)(info->adp, newbank*info->adp->va_window_size);
301 info->bank = newbank;
303 sofs %= info->adp->va_window_size;
304 bofs = x & 0x7; /* offset within byte */
305 outw(GDCIDX, (0x8000 >> bofs) | 0x08); /* bit mask */
306 outw(GDCIDX, (val << 8) | 0x00); /* set/reset */
307 *(info->vidmem + sofs) ^= 0xff; /* read-modify-write */
313 newbank = sofs/info->adp->va_window_size;
314 if (info->bank != newbank) {
315 (*vidsw[info->adp->va_index]->set_win_org)(info->adp, newbank*info->adp->va_window_size);
316 info->bank = newbank;
318 sofs %= info->adp->va_window_size;
319 *(info->vidmem+sofs) = val;
327 ** Given (data) pointing to a line of RLE4-format data and (line) being the starting
328 ** line onscreen, decode the line.
331 bmp_DecodeRLE4(BMP_INFO *info, int line)
333 int count; /* run count */
335 int x,y; /* screen position */
337 x = 0; /* starting position */
340 /* loop reading data */
343 * encoded mode starts with a run length, and then a byte with
344 * two colour indexes to alternate between for the run
347 for (count = 0; count < *info->index; count++, x++) {
348 if (count & 1) { /* odd count, low nybble */
349 bmp_SetPix(info, x, y, *(info->index+1) & 0x0f);
350 } else { /* even count, high nybble */
351 bmp_SetPix(info, x, y, (*(info->index+1) >>4) & 0x0f);
356 * A leading zero is an escape; it may signal the end of the
357 * bitmap, a cursor move, or some absolute data.
359 } else { /* zero tag may be absolute mode or an escape */
360 switch (*(info->index+1)) {
361 case 0: /* end of line */
364 case 1: /* end of bitmap */
368 x += *(info->index + 2); /* new coords */
369 y += *(info->index + 3);
372 default: /* literal bitmap data */
373 for (count = 0; count < *(info->index + 1); count++, x++) {
374 val = *(info->index + 2 + (count / 2)); /* byte with nybbles */
376 val &= 0xf; /* get low nybble */
378 val = (val >> 4); /* get high nybble */
380 bmp_SetPix(info, x, y, val);
382 /* warning, this depends on integer truncation, do not hand-optimise! */
383 info->index += 2 + ((count + 3) / 4) * 2;
392 ** Given (data) pointing to a line of RLE8-format data and (line) being the starting
393 ** line onscreen, decode the line.
396 bmp_DecodeRLE8(BMP_INFO *info, int line)
398 int count; /* run count */
399 int x,y; /* screen position */
401 x = 0; /* starting position */
404 /* loop reading data */
407 * encoded mode starts with a run length, and then a byte with
408 * two colour indexes to alternate between for the run
411 for (count = 0; count < *info->index; count++, x++)
412 bmp_SetPix(info, x, y, *(info->index+1));
415 * A leading zero is an escape; it may signal the end of the
416 * bitmap, a cursor move, or some absolute data.
418 } else { /* zero tag may be absolute mode or an escape */
419 switch(*(info->index+1)) {
420 case 0: /* end of line */
423 case 1: /* end of bitmap */
427 x += *(info->index + 2); /* new coords */
428 y += *(info->index + 3);
431 default: /* literal bitmap data */
432 for (count = 0; count < *(info->index + 1); count++, x++)
433 bmp_SetPix(info, x, y, *(info->index + 2 + count));
434 /* must be an even count */
435 info->index += 2 + count + (count & 1);
445 ** Given (info) pointing to an image being decoded, (line) being the line currently
446 ** being displayed, decode a line of data.
449 bmp_DecodeLine(BMP_INFO *info, int line)
452 u_char val, mask, *p;
454 switch(info->format) {
456 switch(info->depth) {
458 for (x = 0; x < info->width; x++, info->index++)
459 bmp_SetPix(info, x, line, *info->index);
460 info->index += 3 - (--x % 4);
464 for (x = 0; x < info->width; x++) {
466 val = *p & 0xf; /* get low nybble */
469 val = *p >> 4; /* get high nybble */
471 bmp_SetPix(info, x, line, val);
473 /* warning, this depends on integer truncation, do not hand-optimise! */
474 info->index += ((x + 7) / 8) * 4;
479 for (x = 0; x < info->width; x++) {
480 val = (*p & mask) ? 1 : 0;
486 bmp_SetPix(info, x, line, val);
488 /* warning, this depends on integer truncation, do not hand-optimise! */
489 info->index += ((x + 31) / 32) * 4;
494 bmp_DecodeRLE4(info, line);
497 bmp_DecodeRLE8(info, line);
505 ** Given a pointer (data) to the image of a BMP file, fill in bmp_info with what
506 ** can be learnt from it. Return nonzero if the file isn't usable.
508 ** Take screen dimensions (swidth), (sheight) and (sdepth) and make sure we
509 ** can work with these.
512 bmp_Init(const char *data, int swidth, int sheight, int sdepth)
514 BITMAPF *bmf = (BITMAPF *)data;
517 bmp_info.data = NULL; /* assume setup failed */
520 if (bmf->bmfh.bfType != 0x4d42) {
521 printf("splash_bmp: not a BMP file\n");
522 return(1); /* XXX check word ordering for big-endian ports? */
525 /* do we understand this bitmap format? */
526 if (bmf->bmfi.bmiHeader.biSize > sizeof(bmf->bmfi.bmiHeader)) {
527 printf("splash_bmp: unsupported BMP format (size=%d)\n",
528 bmf->bmfi.bmiHeader.biSize);
532 /* save what we know about the screen */
533 bmp_info.swidth = swidth;
534 bmp_info.sheight = sheight;
535 bmp_info.sdepth = sdepth;
537 /* where's the data? */
538 bmp_info.data = (u_char *)data + bmf->bmfh.bfOffBits;
540 /* image parameters */
541 bmp_info.width = bmf->bmfi.bmiHeader.biWidth;
542 bmp_info.height = bmf->bmfi.bmiHeader.biHeight;
543 bmp_info.depth = bmf->bmfi.bmiHeader.biBitCount;
544 bmp_info.format = bmf->bmfi.bmiHeader.biCompression;
546 switch(bmp_info.format) { /* check compression format */
552 printf("splash_bmp: unsupported compression format\n");
553 return(1); /* unsupported compression format */
556 /* palette details */
557 bmp_info.ncols = (bmf->bmfi.bmiHeader.biClrUsed);
558 bzero(bmp_info.palette,sizeof(bmp_info.palette));
559 if (bmp_info.ncols == 0) { /* uses all of them */
560 bmp_info.ncols = 1 << bmf->bmfi.bmiHeader.biBitCount;
562 if ((bmp_info.height > bmp_info.sheight) ||
563 (bmp_info.width > bmp_info.swidth) ||
564 (bmp_info.ncols > (1 << sdepth))) {
566 printf("splash_bmp: beyond screen capacity (%dx%d, %d colors)\n",
567 bmp_info.width, bmp_info.height, bmp_info.ncols);
572 for (pind = 0; pind < bmp_info.ncols; pind++) {
573 bmp_info.palette[pind][0] = bmf->bmfi.bmiColors[pind].rgbRed;
574 bmp_info.palette[pind][1] = bmf->bmfi.bmiColors[pind].rgbGreen;
575 bmp_info.palette[pind][2] = bmf->bmfi.bmiColors[pind].rgbBlue;
583 ** Render the image. Return nonzero if that's not possible.
587 bmp_Draw(video_adapter_t *adp)
596 if (bmp_info.data == NULL) { /* init failed, do nothing */
600 /* clear the screen */
601 bmp_info.vidmem = (u_char *)adp->va_window;
603 (*vidsw[adp->va_index]->clear)(adp);
604 (*vidsw[adp->va_index]->set_win_org)(adp, 0);
607 /* initialise the info structure for drawing */
608 bmp_info.index = bmp_info.data;
610 bmp_info.prev_val = 255;
613 /* set the palette for our image */
614 (*vidsw[adp->va_index]->load_palette)(adp, (u_char *)&bmp_info.palette);
618 /* XXX: this is ugly, but necessary for EGA/VGA 1bpp/4bpp modes */
619 if ((adp->va_type == KD_EGA) || (adp->va_type == KD_VGA)) {
620 inb(adp->va_crtc_addr + 6); /* reset flip-flop */
623 for (i = 0; i < 16; ++i) {
627 inb(adp->va_crtc_addr + 6); /* reset flip-flop */
628 outb(ATC, 0x20); /* enable palette */
630 outw(GDCIDX, 0x0f01); /* set/reset enable */
632 if (bmp_info.sdepth == 1)
633 outw(TSIDX, 0x0102); /* unmask plane #0 */
638 for (line = 0; (line < bmp_info.height) && bmp_info.index; line++) {
639 bmp_DecodeLine(&bmp_info, line);