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