Home | History | Annotate | Line # | Download | only in rasops
rasops.c revision 1.34
      1 /*	 $NetBSD: rasops.c,v 1.34 2001/01/21 13:50:59 takemura Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: rasops.c,v 1.34 2001/01/21 13:50:59 takemura Exp $");
     41 
     42 #include "opt_rasops.h"
     43 #include "rasops_glue.h"
     44 
     45 #include <sys/types.h>
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/time.h>
     49 
     50 #include <machine/bswap.h>
     51 #include <machine/endian.h>
     52 
     53 #include <dev/wscons/wsdisplayvar.h>
     54 #include <dev/wscons/wsconsio.h>
     55 #include <dev/wsfont/wsfont.h>
     56 #include <dev/rasops/rasops.h>
     57 
     58 #ifndef _KERNEL
     59 #include <errno.h>
     60 #endif
     61 
     62 /* ANSI colormap (R,G,B). Upper 8 are high-intensity */
     63 const u_char rasops_cmap[256*3] = {
     64 	0x00, 0x00, 0x00, /* black */
     65 	0x7f, 0x00, 0x00, /* red */
     66 	0x00, 0x7f, 0x00, /* green */
     67 	0x7f, 0x7f, 0x00, /* brown */
     68 	0x00, 0x00, 0x7f, /* blue */
     69 	0x7f, 0x00, 0x7f, /* magenta */
     70 	0x00, 0x7f, 0x7f, /* cyan */
     71 	0xc7, 0xc7, 0xc7, /* white - XXX too dim? */
     72 
     73 	0x7f, 0x7f, 0x7f, /* black */
     74 	0xff, 0x00, 0x00, /* red */
     75 	0x00, 0xff, 0x00, /* green */
     76 	0xff, 0xff, 0x00, /* brown */
     77 	0x00, 0x00, 0xff, /* blue */
     78 	0xff, 0x00, 0xff, /* magenta */
     79 	0x00, 0xff, 0xff, /* cyan */
     80 	0xff, 0xff, 0xff, /* white */
     81 
     82 	/*
     83 	 * For the cursor, we need at least the last (255th)
     84 	 * color to be white. Fill up white completely for
     85 	 * simplicity.
     86 	 */
     87 #define _CMWHITE 0xff, 0xff, 0xff,
     88 #define _CMWHITE16	_CMWHITE _CMWHITE _CMWHITE _CMWHITE \
     89 			_CMWHITE _CMWHITE _CMWHITE _CMWHITE \
     90 			_CMWHITE _CMWHITE _CMWHITE _CMWHITE \
     91 			_CMWHITE _CMWHITE _CMWHITE _CMWHITE
     92 	_CMWHITE16 _CMWHITE16 _CMWHITE16 _CMWHITE16 _CMWHITE16
     93 	_CMWHITE16 _CMWHITE16 _CMWHITE16 _CMWHITE16 _CMWHITE16
     94 	_CMWHITE16 _CMWHITE16 _CMWHITE16 _CMWHITE16 _CMWHITE16
     95 #undef _CMWHITE16
     96 #undef _CMWHITE
     97 };
     98 
     99 /* True if color is gray */
    100 const u_char rasops_isgray[16] = {
    101 	1, 0, 0, 0,
    102 	0, 0, 0, 1,
    103 	1, 0, 0, 0,
    104 	0, 0, 0, 1
    105 };
    106 
    107 /* Generic functions */
    108 static void	rasops_copyrows __P((void *, int, int, int));
    109 static int	rasops_mapchar __P((void *, int, u_int *));
    110 static void	rasops_cursor __P((void *, int, int, int));
    111 static int	rasops_alloc_cattr __P((void *, int, int, int, long *));
    112 static int	rasops_alloc_mattr __P((void *, int, int, int, long *));
    113 static void	rasops_do_cursor __P((struct rasops_info *));
    114 static void	rasops_init_devcmap __P((struct rasops_info *));
    115 
    116 /*
    117  * Initalize a 'rasops_info' descriptor.
    118  */
    119 int
    120 rasops_init(ri, wantrows, wantcols)
    121 	struct rasops_info *ri;
    122 	int wantrows, wantcols;
    123 {
    124 
    125 #ifdef _KERNEL
    126 	/* Select a font if the caller doesn't care */
    127 	if (ri->ri_font == NULL) {
    128 		int cookie;
    129 
    130 		wsfont_init();
    131 
    132 		/* Want 8 pixel wide, don't care about aestethics */
    133 		if ((cookie = wsfont_find(NULL, 8, 0, 0)) <= 0)
    134 			cookie = wsfont_find(NULL, 0, 0, 0);
    135 
    136 		if (cookie <= 0) {
    137 			printf("rasops_init: font table is empty\n");
    138 			return (-1);
    139 		}
    140 
    141 		if (wsfont_lock(cookie, &ri->ri_font,
    142 		    WSDISPLAY_FONTORDER_L2R, WSDISPLAY_FONTORDER_L2R) <= 0) {
    143 			printf("rasops_init: couldn't lock font\n");
    144 			return (-1);
    145 		}
    146 
    147 		ri->ri_wsfcookie = cookie;
    148 	}
    149 #endif
    150 
    151 	/* This should never happen in reality... */
    152 #ifdef DEBUG
    153 	if ((long)ri->ri_bits & 3) {
    154 		printf("rasops_init: bits not aligned on 32-bit boundary\n");
    155 		return (-1);
    156 	}
    157 
    158 	if ((int)ri->ri_stride & 3) {
    159 		printf("rasops_init: stride not aligned on 32-bit boundary\n");
    160 		return (-1);
    161 	}
    162 #endif
    163 
    164 	if (rasops_reconfig(ri, wantrows, wantcols))
    165 		return (-1);
    166 
    167 	rasops_init_devcmap(ri);
    168 	return (0);
    169 }
    170 
    171 /*
    172  * Reconfigure (because parameters have changed in some way).
    173  */
    174 int
    175 rasops_reconfig(ri, wantrows, wantcols)
    176 	struct rasops_info *ri;
    177 	int wantrows, wantcols;
    178 {
    179 	int bpp, s;
    180 
    181 	s = splhigh();
    182 
    183 	if (ri->ri_font->fontwidth > 32 || ri->ri_font->fontwidth < 4)
    184 		panic("rasops_init: fontwidth assumptions botched!\n");
    185 
    186 	/* Need this to frob the setup below */
    187 	bpp = (ri->ri_depth == 15 ? 16 : ri->ri_depth);
    188 
    189 	if ((ri->ri_flg & RI_CFGDONE) != 0)
    190 		ri->ri_bits = ri->ri_origbits;
    191 
    192 	/* Don't care if the caller wants a hideously small console */
    193 	if (wantrows < 10)
    194 		wantrows = 10;
    195 
    196 	if (wantcols < 20)
    197 		wantcols = 20;
    198 
    199 	/* Now constrain what they get */
    200 	ri->ri_emuwidth = ri->ri_font->fontwidth * wantcols;
    201 	ri->ri_emuheight = ri->ri_font->fontheight * wantrows;
    202 
    203 	if (ri->ri_emuwidth > ri->ri_width)
    204 		ri->ri_emuwidth = ri->ri_width;
    205 
    206 	if (ri->ri_emuheight > ri->ri_height)
    207 		ri->ri_emuheight = ri->ri_height;
    208 
    209 	/* Reduce width until aligned on a 32-bit boundary */
    210 	while ((ri->ri_emuwidth * bpp & 31) != 0)
    211 		ri->ri_emuwidth--;
    212 
    213 	ri->ri_cols = ri->ri_emuwidth / ri->ri_font->fontwidth;
    214 	ri->ri_rows = ri->ri_emuheight / ri->ri_font->fontheight;
    215 	ri->ri_emustride = ri->ri_emuwidth * bpp >> 3;
    216 	ri->ri_delta = ri->ri_stride - ri->ri_emustride;
    217 	ri->ri_ccol = 0;
    218 	ri->ri_crow = 0;
    219 	ri->ri_pelbytes = bpp >> 3;
    220 
    221 	ri->ri_xscale = (ri->ri_font->fontwidth * bpp) >> 3;
    222 	ri->ri_yscale = ri->ri_font->fontheight * ri->ri_stride;
    223 	ri->ri_fontscale = ri->ri_font->fontheight * ri->ri_font->stride;
    224 
    225 #ifdef DEBUG
    226 	if ((ri->ri_delta & 3) != 0)
    227 		panic("rasops_init: ri_delta not aligned on 32-bit boundary");
    228 #endif
    229 	/* Clear the entire display */
    230 	if ((ri->ri_flg & RI_CLEAR) != 0)
    231 		memset(ri->ri_bits, 0, ri->ri_stride * ri->ri_height);
    232 
    233 	/* Now centre our window if needs be */
    234 	ri->ri_origbits = ri->ri_bits;
    235 
    236 	if ((ri->ri_flg & RI_CENTER) != 0) {
    237 		ri->ri_bits += ((ri->ri_width - ri->ri_emustride) >> 1) & ~3;
    238 		ri->ri_bits += ((ri->ri_height - ri->ri_emuheight) >> 1) *
    239 		    ri->ri_stride;
    240 
    241 		ri->ri_yorigin = (int)(ri->ri_bits - ri->ri_origbits)
    242 		   / ri->ri_stride;
    243 		ri->ri_xorigin = (((int)(ri->ri_bits - ri->ri_origbits)
    244 		   % ri->ri_stride) * 8 / bpp);
    245 	} else
    246 		ri->ri_xorigin = ri->ri_yorigin = 0;
    247 
    248 	/*
    249 	 * Fill in defaults for operations set.  XXX this nukes private
    250 	 * routines used by accelerated fb drivers.
    251 	 */
    252 	ri->ri_ops.mapchar = rasops_mapchar;
    253 	ri->ri_ops.copyrows = rasops_copyrows;
    254 	ri->ri_ops.copycols = rasops_copycols;
    255 	ri->ri_ops.erasecols = rasops_erasecols;
    256 	ri->ri_ops.eraserows = rasops_eraserows;
    257 	ri->ri_ops.cursor = rasops_cursor;
    258 	ri->ri_do_cursor = rasops_do_cursor;
    259 
    260 	if (ri->ri_depth < 8 || (ri->ri_flg & RI_FORCEMONO) != 0) {
    261 		ri->ri_ops.alloc_attr = rasops_alloc_mattr;
    262 		ri->ri_caps = WSSCREEN_UNDERLINE | WSSCREEN_REVERSE;
    263 	} else {
    264 		ri->ri_ops.alloc_attr = rasops_alloc_cattr;
    265 		ri->ri_caps = WSSCREEN_UNDERLINE | WSSCREEN_HILIT |
    266 		    WSSCREEN_WSCOLORS | WSSCREEN_REVERSE;
    267 	}
    268 
    269 	switch (ri->ri_depth) {
    270 #if NRASOPS1 > 0
    271 	case 1:
    272 		rasops1_init(ri);
    273 		break;
    274 #endif
    275 #if NRASOPS2 > 0
    276 	case 2:
    277 		rasops2_init(ri);
    278 		break;
    279 #endif
    280 #if NRASOPS4 > 0
    281 	case 4:
    282 		rasops4_init(ri);
    283 		break;
    284 #endif
    285 #if NRASOPS8 > 0
    286 	case 8:
    287 		rasops8_init(ri);
    288 		break;
    289 #endif
    290 #if NRASOPS15 > 0 || NRASOPS16 > 0
    291 	case 15:
    292 	case 16:
    293 		rasops15_init(ri);
    294 		break;
    295 #endif
    296 #if NRASOPS24 > 0
    297 	case 24:
    298 		rasops24_init(ri);
    299 		break;
    300 #endif
    301 #if NRASOPS32 > 0
    302 	case 32:
    303 		rasops32_init(ri);
    304 		break;
    305 #endif
    306 	default:
    307 		ri->ri_flg &= ~RI_CFGDONE;
    308 		splx(s);
    309 		return (-1);
    310 	}
    311 
    312 	ri->ri_flg |= RI_CFGDONE;
    313 	splx(s);
    314 	return (0);
    315 }
    316 
    317 /*
    318  * Map a character.
    319  */
    320 static int
    321 rasops_mapchar(cookie, c, cp)
    322 	void *cookie;
    323 	int c;
    324 	u_int *cp;
    325 {
    326 	struct rasops_info *ri;
    327 
    328 	ri = (struct rasops_info *)cookie;
    329 
    330 #ifdef DIAGNOSTIC
    331 	if (ri->ri_font == NULL)
    332 		panic("rasops_mapchar: no font selected\n");
    333 #endif
    334 
    335 	if (c < ri->ri_font->firstchar) {
    336 		*cp = ' ';
    337 		return (0);
    338 	}
    339 
    340 	if (c - ri->ri_font->firstchar >= ri->ri_font->numchars) {
    341 		*cp = ' ';
    342 		return (0);
    343 	}
    344 
    345 	*cp = c;
    346 	return (5);
    347 }
    348 
    349 /*
    350  * Allocate a color attribute.
    351  */
    352 static int
    353 rasops_alloc_cattr(cookie, fg, bg, flg, attr)
    354 	void *cookie;
    355 	int fg, bg, flg;
    356 	long *attr;
    357 {
    358 	int swap;
    359 
    360 #ifdef RASOPS_CLIPPING
    361 	fg &= 7;
    362 	bg &= 7;
    363 #endif
    364 	if ((flg & WSATTR_BLINK) != 0)
    365 		return (EINVAL);
    366 
    367 	if ((flg & WSATTR_WSCOLORS) == 0) {
    368 		fg = WSCOL_WHITE;
    369 		bg = WSCOL_BLACK;
    370 	}
    371 
    372 	if ((flg & WSATTR_REVERSE) != 0) {
    373 		swap = fg;
    374 		fg = bg;
    375 		bg = swap;
    376 	}
    377 
    378 	if ((flg & WSATTR_HILIT) != 0)
    379 		fg += 8;
    380 
    381 	flg = ((flg & WSATTR_UNDERLINE) ? 1 : 0);
    382 
    383 	if (rasops_isgray[fg])
    384 		flg |= 2;
    385 
    386 	if (rasops_isgray[bg])
    387 		flg |= 4;
    388 
    389 	*attr = (bg << 16) | (fg << 24) | flg;
    390 	return (0);
    391 }
    392 
    393 /*
    394  * Allocate a mono attribute.
    395  */
    396 static int
    397 rasops_alloc_mattr(cookie, fg, bg, flg, attr)
    398 	void *cookie;
    399 	int fg, bg, flg;
    400 	long *attr;
    401 {
    402 	int swap;
    403 
    404 	if ((flg & (WSATTR_BLINK | WSATTR_HILIT | WSATTR_WSCOLORS)) != 0)
    405 		return (EINVAL);
    406 
    407 	fg = 1;
    408 	bg = 0;
    409 
    410 	if ((flg & WSATTR_REVERSE) != 0) {
    411 		swap = fg;
    412 		fg = bg;
    413 		bg = swap;
    414 	}
    415 
    416 	*attr = (bg << 16) | (fg << 24) | ((flg & WSATTR_UNDERLINE) ? 7 : 6);
    417 	return (0);
    418 }
    419 
    420 /*
    421  * Copy rows.
    422  */
    423 static void
    424 rasops_copyrows(cookie, src, dst, num)
    425 	void *cookie;
    426 	int src, dst, num;
    427 {
    428 	int32_t *sp, *dp, *srp, *drp;
    429 	struct rasops_info *ri;
    430 	int n8, n1, cnt, delta;
    431 
    432 	ri = (struct rasops_info *)cookie;
    433 
    434 #ifdef RASOPS_CLIPPING
    435 	if (dst == src)
    436 		return;
    437 
    438 	if (src < 0) {
    439 		num += src;
    440 		src = 0;
    441 	}
    442 
    443 	if ((src + num) > ri->ri_rows)
    444 		num = ri->ri_rows - src;
    445 
    446 	if (dst < 0) {
    447 		num += dst;
    448 		dst = 0;
    449 	}
    450 
    451 	if ((dst + num) > ri->ri_rows)
    452 		num = ri->ri_rows - dst;
    453 
    454 	if (num <= 0)
    455 		return;
    456 #endif
    457 
    458 	num *= ri->ri_font->fontheight;
    459 	n8 = ri->ri_emustride >> 5;
    460 	n1 = (ri->ri_emustride >> 2) & 7;
    461 
    462 	if (dst < src) {
    463 		srp = (int32_t *)(ri->ri_bits + src * ri->ri_yscale);
    464 		drp = (int32_t *)(ri->ri_bits + dst * ri->ri_yscale);
    465 		delta = ri->ri_stride;
    466 	} else {
    467 		src = ri->ri_font->fontheight * src + num - 1;
    468 		dst = ri->ri_font->fontheight * dst + num - 1;
    469 		srp = (int32_t *)(ri->ri_bits + src * ri->ri_stride);
    470 		drp = (int32_t *)(ri->ri_bits + dst * ri->ri_stride);
    471 		delta = -ri->ri_stride;
    472 	}
    473 
    474 	while (num--) {
    475 		dp = drp;
    476 		sp = srp;
    477 		DELTA(drp, delta, int32_t *);
    478 		DELTA(srp, delta, int32_t *);
    479 
    480 		for (cnt = n8; cnt; cnt--) {
    481 			dp[0] = sp[0];
    482 			dp[1] = sp[1];
    483 			dp[2] = sp[2];
    484 			dp[3] = sp[3];
    485 			dp[4] = sp[4];
    486 			dp[5] = sp[5];
    487 			dp[6] = sp[6];
    488 			dp[7] = sp[7];
    489 			dp += 8;
    490 			sp += 8;
    491 		}
    492 
    493 		for (cnt = n1; cnt; cnt--)
    494 			*dp++ = *sp++;
    495 	}
    496 }
    497 
    498 /*
    499  * Copy columns. This is slow, and hard to optimize due to alignment,
    500  * and the fact that we have to copy both left->right and right->left.
    501  * We simply cop-out here and use bcopy(), since it handles all of
    502  * these cases anyway.
    503  */
    504 void
    505 rasops_copycols(cookie, row, src, dst, num)
    506 	void *cookie;
    507 	int row, src, dst, num;
    508 {
    509 	struct rasops_info *ri;
    510 	u_char *sp, *dp;
    511 	int height;
    512 
    513 	ri = (struct rasops_info *)cookie;
    514 
    515 #ifdef RASOPS_CLIPPING
    516 	if (dst == src)
    517 		return;
    518 
    519 	/* Catches < 0 case too */
    520 	if ((unsigned)row >= (unsigned)ri->ri_rows)
    521 		return;
    522 
    523 	if (src < 0) {
    524 		num += src;
    525 		src = 0;
    526 	}
    527 
    528 	if ((src + num) > ri->ri_cols)
    529 		num = ri->ri_cols - src;
    530 
    531 	if (dst < 0) {
    532 		num += dst;
    533 		dst = 0;
    534 	}
    535 
    536 	if ((dst + num) > ri->ri_cols)
    537 		num = ri->ri_cols - dst;
    538 
    539 	if (num <= 0)
    540 		return;
    541 #endif
    542 
    543 	num *= ri->ri_xscale;
    544 	row *= ri->ri_yscale;
    545 	height = ri->ri_font->fontheight;
    546 
    547 	sp = ri->ri_bits + row + src * ri->ri_xscale;
    548 	dp = ri->ri_bits + row + dst * ri->ri_xscale;
    549 
    550 	while (height--) {
    551 		bcopy(sp, dp, num);
    552 		dp += ri->ri_stride;
    553 		sp += ri->ri_stride;
    554 	}
    555 }
    556 
    557 /*
    558  * Turn cursor off/on.
    559  */
    560 static void
    561 rasops_cursor(cookie, on, row, col)
    562 	void *cookie;
    563 	int on, row, col;
    564 {
    565 	struct rasops_info *ri;
    566 
    567 	ri = (struct rasops_info *)cookie;
    568 
    569 	/* Turn old cursor off */
    570 	if ((ri->ri_flg & RI_CURSOR) != 0)
    571 #ifdef RASOPS_CLIPPING
    572 		if ((ri->ri_flg & RI_CURSORCLIP) == 0)
    573 #endif
    574 			ri->ri_do_cursor(ri);
    575 
    576 	/* Select new cursor */
    577 #ifdef RASOPS_CLIPPING
    578 	ri->ri_flg &= ~RI_CURSORCLIP;
    579 
    580 	if (row < 0 || row >= ri->ri_rows)
    581 		ri->ri_flg |= RI_CURSORCLIP;
    582 	else if (col < 0 || col >= ri->ri_cols)
    583 		ri->ri_flg |= RI_CURSORCLIP;
    584 #endif
    585 	ri->ri_crow = row;
    586 	ri->ri_ccol = col;
    587 
    588 	if (on) {
    589 		ri->ri_flg |= RI_CURSOR;
    590 #ifdef RASOPS_CLIPPING
    591 		if ((ri->ri_flg & RI_CURSORCLIP) == 0)
    592 #endif
    593 			ri->ri_do_cursor(ri);
    594 	} else
    595 		ri->ri_flg &= ~RI_CURSOR;
    596 }
    597 
    598 /*
    599  * Make the device colormap
    600  */
    601 static void
    602 rasops_init_devcmap(ri)
    603 	struct rasops_info *ri;
    604 {
    605 	const u_char *p;
    606 	int i, c;
    607 
    608 	switch (ri->ri_depth) {
    609 	case 1:
    610 		ri->ri_devcmap[0] = 0;
    611 		for (i = 1; i < 16; i++)
    612 			ri->ri_devcmap[i] = -1;
    613 		return;
    614 
    615 	case 2:
    616 		for (i = 1; i < 15; i++)
    617 			ri->ri_devcmap[i] = 0xaaaaaaaa;
    618 
    619 		ri->ri_devcmap[0] = 0;
    620 		ri->ri_devcmap[8] = 0x55555555;
    621 		ri->ri_devcmap[15] = -1;
    622 		return;
    623 
    624 	case 8:
    625 		for (i = 0; i < 16; i++)
    626 			ri->ri_devcmap[i] = i | (i<<8) | (i<<16) | (i<<24);
    627 		return;
    628 	}
    629 
    630 	p = rasops_cmap;
    631 
    632 	for (i = 0; i < 16; i++) {
    633 		if (ri->ri_rnum <= 8)
    634 			c = (*p >> (8 - ri->ri_rnum)) << ri->ri_rpos;
    635 		else
    636 			c = (*p << (ri->ri_rnum - 8)) << ri->ri_rpos;
    637 		p++;
    638 
    639 		if (ri->ri_gnum <= 8)
    640 			c |= (*p >> (8 - ri->ri_gnum)) << ri->ri_gpos;
    641 		else
    642 			c |= (*p << (ri->ri_gnum - 8)) << ri->ri_gpos;
    643 		p++;
    644 
    645 		if (ri->ri_bnum <= 8)
    646 			c |= (*p >> (8 - ri->ri_bnum)) << ri->ri_bpos;
    647 		else
    648 			c |= (*p << (ri->ri_bnum - 8)) << ri->ri_bpos;
    649 		p++;
    650 
    651 		/* Fill the word for generic routines, which want this */
    652 		if (ri->ri_depth == 24)
    653 			c = c | ((c & 0xff) << 24);
    654 		else if (ri->ri_depth <= 16)
    655 			c = c | (c << 16);
    656 
    657 		/* 24bpp does bswap on the fly. {32,16,15}bpp do it here. */
    658 		if ((ri->ri_flg & RI_BSWAP) == 0)
    659 			ri->ri_devcmap[i] = c;
    660 		else if (ri->ri_depth == 32)
    661 			ri->ri_devcmap[i] = bswap32(c);
    662 		else if (ri->ri_depth == 16 || ri->ri_depth == 15)
    663 			ri->ri_devcmap[i] = bswap16(c);
    664 		else
    665 			ri->ri_devcmap[i] = c;
    666 	}
    667 }
    668 
    669 /*
    670  * Unpack a rasops attribute
    671  */
    672 void
    673 rasops_unpack_attr(attr, fg, bg, underline)
    674 	long attr;
    675 	int *fg, *bg, *underline;
    676 {
    677 
    678 	*fg = ((u_int)attr >> 24) & 0xf;
    679 	*bg = ((u_int)attr >> 16) & 0xf;
    680 	if (underline != NULL)
    681 		*underline = (u_int)attr & 1;
    682 }
    683 
    684 /*
    685  * Erase rows. This isn't static, since 24-bpp uses it in special cases.
    686  */
    687 void
    688 rasops_eraserows(cookie, row, num, attr)
    689 	void *cookie;
    690 	int row, num;
    691 	long attr;
    692 {
    693 	struct rasops_info *ri;
    694 	int np, nw, cnt, delta;
    695 	int32_t *dp, clr;
    696 
    697 	ri = (struct rasops_info *)cookie;
    698 
    699 #ifdef RASOPS_CLIPPING
    700 	if (row < 0) {
    701 		num += row;
    702 		row = 0;
    703 	}
    704 
    705 	if ((row + num) > ri->ri_rows)
    706 		num = ri->ri_rows - row;
    707 
    708 	if (num <= 0)
    709 		return;
    710 #endif
    711 
    712 	clr = ri->ri_devcmap[(attr >> 16) & 0xf];
    713 
    714 	/*
    715 	 * XXX The wsdisplay_emulops interface seems a little deficient in
    716 	 * that there is no way to clear the *entire* screen. We provide a
    717 	 * workaround here: if the entire console area is being cleared, and
    718 	 * the RI_FULLCLEAR flag is set, clear the entire display.
    719 	 */
    720 	if (num == ri->ri_rows && (ri->ri_flg & RI_FULLCLEAR) != 0) {
    721 		np = ri->ri_stride >> 5;
    722 		nw = (ri->ri_stride >> 2) & 7;
    723 		num = ri->ri_height;
    724 		dp = (int32_t *)ri->ri_origbits;
    725 		delta = 0;
    726 	} else {
    727 		np = ri->ri_emustride >> 5;
    728 		nw = (ri->ri_emustride >> 2) & 7;
    729 		num *= ri->ri_font->fontheight;
    730 		dp = (int32_t *)(ri->ri_bits + row * ri->ri_yscale);
    731 		delta = ri->ri_delta;
    732 	}
    733 
    734 	while (num--) {
    735 		for (cnt = np; cnt; cnt--) {
    736 			dp[0] = clr;
    737 			dp[1] = clr;
    738 			dp[2] = clr;
    739 			dp[3] = clr;
    740 			dp[4] = clr;
    741 			dp[5] = clr;
    742 			dp[6] = clr;
    743 			dp[7] = clr;
    744 			dp += 8;
    745 		}
    746 
    747 		for (cnt = nw; cnt; cnt--) {
    748 			*(int32_t *)dp = clr;
    749 			DELTA(dp, 4, int32_t *);
    750 		}
    751 
    752 		DELTA(dp, delta, int32_t *);
    753 	}
    754 }
    755 
    756 /*
    757  * Actually turn the cursor on or off. This does the dirty work for
    758  * rasops_cursor().
    759  */
    760 static void
    761 rasops_do_cursor(ri)
    762 	struct rasops_info *ri;
    763 {
    764 	int full1, height, cnt, slop1, slop2, row, col;
    765 	u_char *dp, *rp;
    766 
    767 	row = ri->ri_crow;
    768 	col = ri->ri_ccol;
    769 
    770 	rp = ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale;
    771 	height = ri->ri_font->fontheight;
    772 	slop1 = (4 - ((long)rp & 3)) & 3;
    773 
    774 	if (slop1 > ri->ri_xscale)
    775 		slop1 = ri->ri_xscale;
    776 
    777 	slop2 = (ri->ri_xscale - slop1) & 3;
    778 	full1 = (ri->ri_xscale - slop1 - slop2) >> 2;
    779 
    780 	if ((slop1 | slop2) == 0) {
    781 		/* A common case */
    782 		while (height--) {
    783 			dp = rp;
    784 			rp += ri->ri_stride;
    785 
    786 			for (cnt = full1; cnt; cnt--) {
    787 				*(int32_t *)dp ^= ~0;
    788 				dp += 4;
    789 			}
    790 		}
    791 	} else {
    792 		/* XXX this is stupid.. use masks instead */
    793 		while (height--) {
    794 			dp = rp;
    795 			rp += ri->ri_stride;
    796 
    797 			if (slop1 & 1)
    798 				*dp++ ^= ~0;
    799 
    800 			if (slop1 & 2) {
    801 				*(int16_t *)dp ^= ~0;
    802 				dp += 2;
    803 			}
    804 
    805 			for (cnt = full1; cnt; cnt--) {
    806 				*(int32_t *)dp ^= ~0;
    807 				dp += 4;
    808 			}
    809 
    810 			if (slop2 & 1)
    811 				*dp++ ^= ~0;
    812 
    813 			if (slop2 & 2)
    814 				*(int16_t *)dp ^= ~0;
    815 		}
    816 	}
    817 }
    818 
    819 /*
    820  * Erase columns.
    821  */
    822 void
    823 rasops_erasecols(cookie, row, col, num, attr)
    824 	void *cookie;
    825 	int row, col, num;
    826 	long attr;
    827 {
    828 	int n8, height, cnt, slop1, slop2, clr;
    829 	struct rasops_info *ri;
    830 	int32_t *rp, *dp;
    831 
    832 	ri = (struct rasops_info *)cookie;
    833 
    834 #ifdef RASOPS_CLIPPING
    835 	if ((unsigned)row >= (unsigned)ri->ri_rows)
    836 		return;
    837 
    838 	if (col < 0) {
    839 		num += col;
    840 		col = 0;
    841 	}
    842 
    843 	if ((col + num) > ri->ri_cols)
    844 		num = ri->ri_cols - col;
    845 
    846 	if (num <= 0)
    847 		return;
    848 #endif
    849 
    850 	num = num * ri->ri_xscale;
    851 	rp = (int32_t *)(ri->ri_bits + row*ri->ri_yscale + col*ri->ri_xscale);
    852 	height = ri->ri_font->fontheight;
    853 	clr = ri->ri_devcmap[(attr >> 16) & 0xf];
    854 
    855 	/* Don't bother using the full loop for <= 32 pels */
    856 	if (num <= 32) {
    857 		if (((num | ri->ri_xscale) & 3) == 0) {
    858 			/* Word aligned blt */
    859 			num >>= 2;
    860 
    861 			while (height--) {
    862 				dp = rp;
    863 				DELTA(rp, ri->ri_stride, int32_t *);
    864 
    865 				for (cnt = num; cnt; cnt--)
    866 					*dp++ = clr;
    867 			}
    868 		} else if (((num | ri->ri_xscale) & 1) == 0) {
    869 			/*
    870 			 * Halfword aligned blt. This is needed so the
    871 			 * 15/16 bit ops can use this function.
    872 			 */
    873 			num >>= 1;
    874 
    875 			while (height--) {
    876 				dp = rp;
    877 				DELTA(rp, ri->ri_stride, int32_t *);
    878 
    879 				for (cnt = num; cnt; cnt--) {
    880 					*(int16_t *)dp = clr;
    881 					DELTA(dp, 2, int32_t *);
    882 				}
    883 			}
    884 		} else {
    885 			while (height--) {
    886 				dp = rp;
    887 				DELTA(rp, ri->ri_stride, int32_t *);
    888 
    889 				for (cnt = num; cnt; cnt--) {
    890 					*(u_char *)dp = clr;
    891 					DELTA(dp, 1, int32_t *);
    892 				}
    893 			}
    894 		}
    895 
    896 		return;
    897 	}
    898 
    899 	slop1 = (4 - ((long)rp & 3)) & 3;
    900 	slop2 = (num - slop1) & 3;
    901 	num -= slop1 + slop2;
    902 	n8 = num >> 5;
    903 	num = (num >> 2) & 7;
    904 
    905 	while (height--) {
    906 		dp = rp;
    907 		DELTA(rp, ri->ri_stride, int32_t *);
    908 
    909 		/* Align span to 4 bytes */
    910 		if (slop1 & 1) {
    911 			*(u_char *)dp = clr;
    912 			DELTA(dp, 1, int32_t *);
    913 		}
    914 
    915 		if (slop1 & 2) {
    916 			*(int16_t *)dp = clr;
    917 			DELTA(dp, 2, int32_t *);
    918 		}
    919 
    920 		/* Write 32 bytes per loop */
    921 		for (cnt = n8; cnt; cnt--) {
    922 			dp[0] = clr;
    923 			dp[1] = clr;
    924 			dp[2] = clr;
    925 			dp[3] = clr;
    926 			dp[4] = clr;
    927 			dp[5] = clr;
    928 			dp[6] = clr;
    929 			dp[7] = clr;
    930 			dp += 8;
    931 		}
    932 
    933 		/* Write 4 bytes per loop */
    934 		for (cnt = num; cnt; cnt--)
    935 			*dp++ = clr;
    936 
    937 		/* Write unaligned trailing slop */
    938 		if (slop2 & 1) {
    939 			*(u_char *)dp = clr;
    940 			DELTA(dp, 1, int32_t *);
    941 		}
    942 
    943 		if (slop2 & 2)
    944 			*(int16_t *)dp = clr;
    945 	}
    946 }
    947