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