Home | History | Annotate | Line # | Download | only in rasops
rasops.c revision 1.49.2.2
      1 /*	 $NetBSD: rasops.c,v 1.49.2.2 2006/02/18 15:39:11 yamt 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.49.2.2 2006/02/18 15:39:11 yamt 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 <sys/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 #ifdef RASOPS_CLIPPING
    394 	fg &= 7;
    395 	bg &= 7;
    396 #endif
    397 	if ((flg & WSATTR_BLINK) != 0)
    398 		return (EINVAL);
    399 
    400 	if ((flg & WSATTR_WSCOLORS) == 0) {
    401 #ifdef WS_DEFAULT_FG
    402 		fg = WS_DEFAULT_FG;
    403 #else
    404 		fg = WSCOL_WHITE;
    405 #endif
    406 #ifdef WS_DEFAULT_BG
    407 		bg = WS_DEFAULT_BG;
    408 #else
    409 		bg = WSCOL_BLACK;
    410 #endif
    411 	}
    412 
    413 	if ((flg & WSATTR_REVERSE) != 0) {
    414 		swap = fg;
    415 		fg = bg;
    416 		bg = swap;
    417 	}
    418 
    419 	if ((flg & WSATTR_HILIT) != 0)
    420 		fg += 8;
    421 
    422 	flg = ((flg & WSATTR_UNDERLINE) ? 1 : 0);
    423 
    424 	if (rasops_isgray[fg])
    425 		flg |= 2;
    426 
    427 	if (rasops_isgray[bg])
    428 		flg |= 4;
    429 
    430 	*attr = (bg << 16) | (fg << 24) | flg;
    431 	return (0);
    432 }
    433 
    434 /*
    435  * Allocate a mono attribute.
    436  */
    437 static int
    438 rasops_allocattr_mono(cookie, fg, bg, flg, attr)
    439 	void *cookie;
    440 	int fg, bg, flg;
    441 	long *attr;
    442 {
    443 	int swap;
    444 
    445 	if ((flg & (WSATTR_BLINK | WSATTR_HILIT | WSATTR_WSCOLORS)) != 0)
    446 		return (EINVAL);
    447 
    448 	fg = 1;
    449 	bg = 0;
    450 
    451 	if ((flg & WSATTR_REVERSE) != 0) {
    452 		swap = fg;
    453 		fg = bg;
    454 		bg = swap;
    455 	}
    456 
    457 	*attr = (bg << 16) | (fg << 24) | ((flg & WSATTR_UNDERLINE) ? 7 : 6);
    458 	return (0);
    459 }
    460 
    461 /*
    462  * Copy rows.
    463  */
    464 static void
    465 rasops_copyrows(cookie, src, dst, num)
    466 	void *cookie;
    467 	int src, dst, num;
    468 {
    469 	int32_t *sp, *dp, *hp, *srp, *drp, *hrp;
    470 	struct rasops_info *ri;
    471 	int n8, n1, cnt, delta;
    472 	int i;
    473 
    474 	ri = (struct rasops_info *)cookie;
    475 	hp = hrp = NULL;
    476 
    477 #ifdef RASOPS_CLIPPING
    478 	if (dst == src)
    479 		return;
    480 
    481 	if (src < 0) {
    482 		num += src;
    483 		src = 0;
    484 	}
    485 
    486 	if ((src + num) > ri->ri_rows)
    487 		num = ri->ri_rows - src;
    488 
    489 	if (dst < 0) {
    490 		num += dst;
    491 		dst = 0;
    492 	}
    493 
    494 	if ((dst + num) > ri->ri_rows)
    495 		num = ri->ri_rows - dst;
    496 
    497 	if (num <= 0)
    498 		return;
    499 #endif
    500 
    501 	num *= ri->ri_font->fontheight;
    502 	n8 = ri->ri_emustride >> 5;
    503 	n1 = (ri->ri_emustride >> 2) & 7;
    504 
    505 	if (dst < src) {
    506 		srp = (int32_t *)(ri->ri_bits + src * ri->ri_yscale);
    507 		drp = (int32_t *)(ri->ri_bits + dst * ri->ri_yscale);
    508 		if (ri->ri_hwbits)
    509 			hrp = (int32_t *)(ri->ri_hwbits + dst *
    510 			    ri->ri_yscale);
    511 		delta = ri->ri_stride;
    512 	} else {
    513 		src = ri->ri_font->fontheight * src + num - 1;
    514 		dst = ri->ri_font->fontheight * dst + num - 1;
    515 		srp = (int32_t *)(ri->ri_bits + src * ri->ri_stride);
    516 		drp = (int32_t *)(ri->ri_bits + dst * ri->ri_stride);
    517 		if (ri->ri_hwbits)
    518 			hrp = (int32_t *)(ri->ri_hwbits + dst *
    519 			    ri->ri_stride);
    520 
    521 		delta = -ri->ri_stride;
    522 	}
    523 
    524 	while (num--) {
    525 		dp = drp;
    526 		sp = srp;
    527 		if (ri->ri_hwbits)
    528 			hp = hrp;
    529 
    530 		DELTA(drp, delta, int32_t *);
    531 		DELTA(srp, delta, int32_t *);
    532 		if (ri->ri_hwbits)
    533 			DELTA(hrp, delta, int32_t *);
    534 
    535 		for (cnt = n8; cnt; cnt--) {
    536 			for (i = 0; i < 8; i++) {
    537 				dp[i] = sp[i];
    538 				if (ri->ri_hwbits)
    539 					hp[i] = sp[i];
    540 			}
    541 			dp += 8;
    542 			sp += 8;
    543 			if (ri->ri_hwbits)
    544 				hp += 8;
    545 		}
    546 
    547 		for (cnt = n1; cnt; cnt--) {
    548 			*dp++ = *sp++;
    549 			if (ri->ri_hwbits)
    550 				*hp++ = *(sp - 1);
    551 		}
    552 	}
    553 }
    554 
    555 /*
    556  * Copy columns. This is slow, and hard to optimize due to alignment,
    557  * and the fact that we have to copy both left->right and right->left.
    558  * We simply cop-out here and use memmove(), since it handles all of
    559  * these cases anyway.
    560  */
    561 void
    562 rasops_copycols(cookie, row, src, dst, num)
    563 	void *cookie;
    564 	int row, src, dst, num;
    565 {
    566 	struct rasops_info *ri;
    567 	u_char *sp, *dp, *hp;
    568 	int height;
    569 
    570 	ri = (struct rasops_info *)cookie;
    571 	hp = NULL;
    572 
    573 #ifdef RASOPS_CLIPPING
    574 	if (dst == src)
    575 		return;
    576 
    577 	/* Catches < 0 case too */
    578 	if ((unsigned)row >= (unsigned)ri->ri_rows)
    579 		return;
    580 
    581 	if (src < 0) {
    582 		num += src;
    583 		src = 0;
    584 	}
    585 
    586 	if ((src + num) > ri->ri_cols)
    587 		num = ri->ri_cols - src;
    588 
    589 	if (dst < 0) {
    590 		num += dst;
    591 		dst = 0;
    592 	}
    593 
    594 	if ((dst + num) > ri->ri_cols)
    595 		num = ri->ri_cols - dst;
    596 
    597 	if (num <= 0)
    598 		return;
    599 #endif
    600 
    601 	num *= ri->ri_xscale;
    602 	row *= ri->ri_yscale;
    603 	height = ri->ri_font->fontheight;
    604 
    605 	sp = ri->ri_bits + row + src * ri->ri_xscale;
    606 	dp = ri->ri_bits + row + dst * ri->ri_xscale;
    607 	if (ri->ri_hwbits)
    608 		hp = ri->ri_hwbits + row + dst * ri->ri_xscale;
    609 
    610 	while (height--) {
    611 		memmove(dp, sp, num);
    612 		if (ri->ri_hwbits) {
    613 			memcpy(hp, sp, num);
    614 			hp += ri->ri_stride;
    615 		}
    616 		dp += ri->ri_stride;
    617 		sp += ri->ri_stride;
    618 	}
    619 }
    620 
    621 /*
    622  * Turn cursor off/on.
    623  */
    624 static void
    625 rasops_cursor(cookie, on, row, col)
    626 	void *cookie;
    627 	int on, row, col;
    628 {
    629 	struct rasops_info *ri;
    630 
    631 	ri = (struct rasops_info *)cookie;
    632 
    633 	/* Turn old cursor off */
    634 	if ((ri->ri_flg & RI_CURSOR) != 0)
    635 #ifdef RASOPS_CLIPPING
    636 		if ((ri->ri_flg & RI_CURSORCLIP) == 0)
    637 #endif
    638 			ri->ri_do_cursor(ri);
    639 
    640 	/* Select new cursor */
    641 #ifdef RASOPS_CLIPPING
    642 	ri->ri_flg &= ~RI_CURSORCLIP;
    643 
    644 	if (row < 0 || row >= ri->ri_rows)
    645 		ri->ri_flg |= RI_CURSORCLIP;
    646 	else if (col < 0 || col >= ri->ri_cols)
    647 		ri->ri_flg |= RI_CURSORCLIP;
    648 #endif
    649 	ri->ri_crow = row;
    650 	ri->ri_ccol = col;
    651 
    652 	if (on) {
    653 		ri->ri_flg |= RI_CURSOR;
    654 #ifdef RASOPS_CLIPPING
    655 		if ((ri->ri_flg & RI_CURSORCLIP) == 0)
    656 #endif
    657 			ri->ri_do_cursor(ri);
    658 	} else
    659 		ri->ri_flg &= ~RI_CURSOR;
    660 }
    661 
    662 /*
    663  * Make the device colormap
    664  */
    665 static void
    666 rasops_init_devcmap(ri)
    667 	struct rasops_info *ri;
    668 {
    669 	const u_char *p;
    670 	int i, c;
    671 
    672 	switch (ri->ri_depth) {
    673 	case 1:
    674 		ri->ri_devcmap[0] = 0;
    675 		for (i = 1; i < 16; i++)
    676 			ri->ri_devcmap[i] = -1;
    677 		return;
    678 
    679 	case 2:
    680 		for (i = 1; i < 15; i++)
    681 			ri->ri_devcmap[i] = 0xaaaaaaaa;
    682 
    683 		ri->ri_devcmap[0] = 0;
    684 		ri->ri_devcmap[8] = 0x55555555;
    685 		ri->ri_devcmap[15] = -1;
    686 		return;
    687 
    688 	case 8:
    689 		for (i = 0; i < 16; i++)
    690 			ri->ri_devcmap[i] = i | (i<<8) | (i<<16) | (i<<24);
    691 		return;
    692 	}
    693 
    694 	p = rasops_cmap;
    695 
    696 	for (i = 0; i < 16; i++) {
    697 		if (ri->ri_rnum <= 8)
    698 			c = (*p >> (8 - ri->ri_rnum)) << ri->ri_rpos;
    699 		else
    700 			c = (*p << (ri->ri_rnum - 8)) << ri->ri_rpos;
    701 		p++;
    702 
    703 		if (ri->ri_gnum <= 8)
    704 			c |= (*p >> (8 - ri->ri_gnum)) << ri->ri_gpos;
    705 		else
    706 			c |= (*p << (ri->ri_gnum - 8)) << ri->ri_gpos;
    707 		p++;
    708 
    709 		if (ri->ri_bnum <= 8)
    710 			c |= (*p >> (8 - ri->ri_bnum)) << ri->ri_bpos;
    711 		else
    712 			c |= (*p << (ri->ri_bnum - 8)) << ri->ri_bpos;
    713 		p++;
    714 
    715 		/* Fill the word for generic routines, which want this */
    716 		if (ri->ri_depth == 24)
    717 			c = c | ((c & 0xff) << 24);
    718 		else if (ri->ri_depth <= 16)
    719 			c = c | (c << 16);
    720 
    721 		/* 24bpp does bswap on the fly. {32,16,15}bpp do it here. */
    722 		if ((ri->ri_flg & RI_BSWAP) == 0)
    723 			ri->ri_devcmap[i] = c;
    724 		else if (ri->ri_depth == 32)
    725 			ri->ri_devcmap[i] = bswap32(c);
    726 		else if (ri->ri_depth == 16 || ri->ri_depth == 15)
    727 			ri->ri_devcmap[i] = bswap16(c);
    728 		else
    729 			ri->ri_devcmap[i] = c;
    730 	}
    731 }
    732 
    733 /*
    734  * Unpack a rasops attribute
    735  */
    736 void
    737 rasops_unpack_attr(attr, fg, bg, underline)
    738 	long attr;
    739 	int *fg, *bg, *underline;
    740 {
    741 
    742 	*fg = ((u_int)attr >> 24) & 0xf;
    743 	*bg = ((u_int)attr >> 16) & 0xf;
    744 	if (underline != NULL)
    745 		*underline = (u_int)attr & 1;
    746 }
    747 
    748 /*
    749  * Erase rows. This isn't static, since 24-bpp uses it in special cases.
    750  */
    751 void
    752 rasops_eraserows(cookie, row, num, attr)
    753 	void *cookie;
    754 	int row, num;
    755 	long attr;
    756 {
    757 	struct rasops_info *ri;
    758 	int np, nw, cnt, delta;
    759 	int32_t *dp, *hp, clr;
    760 	int i;
    761 
    762 	ri = (struct rasops_info *)cookie;
    763 	hp = NULL;
    764 
    765 #ifdef RASOPS_CLIPPING
    766 	if (row < 0) {
    767 		num += row;
    768 		row = 0;
    769 	}
    770 
    771 	if ((row + num) > ri->ri_rows)
    772 		num = ri->ri_rows - row;
    773 
    774 	if (num <= 0)
    775 		return;
    776 #endif
    777 
    778 	clr = ri->ri_devcmap[(attr >> 16) & 0xf];
    779 
    780 	/*
    781 	 * XXX The wsdisplay_emulops interface seems a little deficient in
    782 	 * that there is no way to clear the *entire* screen. We provide a
    783 	 * workaround here: if the entire console area is being cleared, and
    784 	 * the RI_FULLCLEAR flag is set, clear the entire display.
    785 	 */
    786 	if (num == ri->ri_rows && (ri->ri_flg & RI_FULLCLEAR) != 0) {
    787 		np = ri->ri_stride >> 5;
    788 		nw = (ri->ri_stride >> 2) & 7;
    789 		num = ri->ri_height;
    790 		dp = (int32_t *)ri->ri_origbits;
    791 		if (ri->ri_hwbits)
    792 			hp = (int32_t *)ri->ri_hwbits;
    793 		delta = 0;
    794 	} else {
    795 		np = ri->ri_emustride >> 5;
    796 		nw = (ri->ri_emustride >> 2) & 7;
    797 		num *= ri->ri_font->fontheight;
    798 		dp = (int32_t *)(ri->ri_bits + row * ri->ri_yscale);
    799 		if (ri->ri_hwbits)
    800 			hp = (int32_t *)(ri->ri_hwbits + row *
    801 			    ri->ri_yscale);
    802 		delta = ri->ri_delta;
    803 	}
    804 
    805 	while (num--) {
    806 		for (cnt = np; cnt; cnt--) {
    807 			for (i = 0; i < 8; i++) {
    808 				dp[i] = clr;
    809 				if (ri->ri_hwbits)
    810 					hp[i] = clr;
    811 			}
    812 			dp += 8;
    813 			if (ri->ri_hwbits)
    814 				hp += 8;
    815 		}
    816 
    817 		for (cnt = nw; cnt; cnt--) {
    818 			*(int32_t *)dp = clr;
    819 			DELTA(dp, 4, int32_t *);
    820 			if (ri->ri_hwbits) {
    821 				*(int32_t *)hp = clr;
    822 				DELTA(hp, 4, int32_t *);
    823 			}
    824 		}
    825 
    826 		DELTA(dp, delta, int32_t *);
    827 		if (ri->ri_hwbits)
    828 			DELTA(hp, delta, int32_t *);
    829 	}
    830 }
    831 
    832 /*
    833  * Actually turn the cursor on or off. This does the dirty work for
    834  * rasops_cursor().
    835  */
    836 static void
    837 rasops_do_cursor(ri)
    838 	struct rasops_info *ri;
    839 {
    840 	int full1, height, cnt, slop1, slop2, row, col;
    841 	u_char *dp, *rp, *hrp, *hp;
    842 
    843 	hrp = hp = NULL;
    844 
    845 	row = ri->ri_crow;
    846 	col = ri->ri_ccol;
    847 
    848 	rp = ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale;
    849 	if (ri->ri_hwbits)
    850 		hrp = ri->ri_hwbits + row * ri->ri_yscale + col
    851 		    * ri->ri_xscale;
    852 	height = ri->ri_font->fontheight;
    853 	slop1 = (4 - ((long)rp & 3)) & 3;
    854 
    855 	if (slop1 > ri->ri_xscale)
    856 		slop1 = ri->ri_xscale;
    857 
    858 	slop2 = (ri->ri_xscale - slop1) & 3;
    859 	full1 = (ri->ri_xscale - slop1 - slop2) >> 2;
    860 
    861 	if ((slop1 | slop2) == 0) {
    862 		/* A common case */
    863 		while (height--) {
    864 			dp = rp;
    865 			rp += ri->ri_stride;
    866 			if (ri->ri_hwbits) {
    867 				hp = hrp;
    868 				hrp += ri->ri_stride;
    869 			}
    870 
    871 			for (cnt = full1; cnt; cnt--) {
    872 				*(int32_t *)dp ^= ~0;
    873 				dp += 4;
    874 				if (ri->ri_hwbits) {
    875 					*(int32_t *)hp ^= ~0;
    876 					hp += 4;
    877 				}
    878 			}
    879 		}
    880 	} else {
    881 		/* XXX this is stupid.. use masks instead */
    882 		while (height--) {
    883 			dp = rp;
    884 			rp += ri->ri_stride;
    885 			if (ri->ri_hwbits) {
    886 				hp = hrp;
    887 				hrp += ri->ri_stride;
    888 			}
    889 
    890 			if (slop1 & 1) {
    891 				*dp++ ^= ~0;
    892 				if (ri->ri_hwbits)
    893 					*hp++ ^= ~0;
    894 			}
    895 
    896 			if (slop1 & 2) {
    897 				*(int16_t *)dp ^= ~0;
    898 				dp += 2;
    899 				if (ri->ri_hwbits) {
    900 					*(int16_t *)hp ^= ~0;
    901 					hp += 2;
    902 				}
    903 			}
    904 
    905 			for (cnt = full1; cnt; cnt--) {
    906 				*(int32_t *)dp ^= ~0;
    907 				dp += 4;
    908 				if (ri->ri_hwbits) {
    909 					*(int32_t *)hp ^= ~0;
    910 					hp += 4;
    911 				}
    912 			}
    913 
    914 			if (slop2 & 1) {
    915 				*dp++ ^= ~0;
    916 				if (ri->ri_hwbits)
    917 					*hp++ ^= ~0;
    918 			}
    919 
    920 			if (slop2 & 2) {
    921 				*(int16_t *)dp ^= ~0;
    922 				if (ri->ri_hwbits)
    923 					*(int16_t *)hp ^= ~0;
    924 			}
    925 		}
    926 	}
    927 }
    928 
    929 /*
    930  * Erase columns.
    931  */
    932 void
    933 rasops_erasecols(cookie, row, col, num, attr)
    934 	void *cookie;
    935 	int row, col, num;
    936 	long attr;
    937 {
    938 	int n8, height, cnt, slop1, slop2, clr;
    939 	struct rasops_info *ri;
    940 	int32_t *rp, *dp, *hrp, *hp;
    941 	int i;
    942 
    943 	ri = (struct rasops_info *)cookie;
    944 	hrp = hp = NULL;
    945 
    946 #ifdef RASOPS_CLIPPING
    947 	if ((unsigned)row >= (unsigned)ri->ri_rows)
    948 		return;
    949 
    950 	if (col < 0) {
    951 		num += col;
    952 		col = 0;
    953 	}
    954 
    955 	if ((col + num) > ri->ri_cols)
    956 		num = ri->ri_cols - col;
    957 
    958 	if (num <= 0)
    959 		return;
    960 #endif
    961 
    962 	num = num * ri->ri_xscale;
    963 	rp = (int32_t *)(ri->ri_bits + row*ri->ri_yscale + col*ri->ri_xscale);
    964 	if (ri->ri_hwbits)
    965 		hrp = (int32_t *)(ri->ri_hwbits + row*ri->ri_yscale +
    966 		    col*ri->ri_xscale);
    967 	height = ri->ri_font->fontheight;
    968 	clr = ri->ri_devcmap[(attr >> 16) & 0xf];
    969 
    970 	/* Don't bother using the full loop for <= 32 pels */
    971 	if (num <= 32) {
    972 		if (((num | ri->ri_xscale) & 3) == 0) {
    973 			/* Word aligned blt */
    974 			num >>= 2;
    975 
    976 			while (height--) {
    977 				dp = rp;
    978 				DELTA(rp, ri->ri_stride, int32_t *);
    979 				if (ri->ri_hwbits) {
    980 					hp = hrp;
    981 					DELTA(hrp, ri->ri_stride, int32_t *);
    982 				}
    983 
    984 				for (cnt = num; cnt; cnt--) {
    985 					*dp++ = clr;
    986 					if (ri->ri_hwbits)
    987 						*hp++ = clr;
    988 				}
    989 			}
    990 		} else if (((num | ri->ri_xscale) & 1) == 0) {
    991 			/*
    992 			 * Halfword aligned blt. This is needed so the
    993 			 * 15/16 bit ops can use this function.
    994 			 */
    995 			num >>= 1;
    996 
    997 			while (height--) {
    998 				dp = rp;
    999 				DELTA(rp, ri->ri_stride, int32_t *);
   1000 				if (ri->ri_hwbits) {
   1001 					hp = hrp;
   1002 					DELTA(hrp, ri->ri_stride, int32_t *);
   1003 				}
   1004 
   1005 				for (cnt = num; cnt; cnt--) {
   1006 					*(int16_t *)dp = clr;
   1007 					DELTA(dp, 2, int32_t *);
   1008 					if (ri->ri_hwbits) {
   1009 						*(int16_t *)hp = clr;
   1010 						DELTA(hp, 2, int32_t *);
   1011 					}
   1012 				}
   1013 			}
   1014 		} else {
   1015 			while (height--) {
   1016 				dp = rp;
   1017 				DELTA(rp, ri->ri_stride, int32_t *);
   1018 				if (ri->ri_hwbits) {
   1019 					hp = hrp;
   1020 					DELTA(hrp, ri->ri_stride, int32_t *);
   1021 				}
   1022 
   1023 				for (cnt = num; cnt; cnt--) {
   1024 					*(u_char *)dp = clr;
   1025 					DELTA(dp, 1, int32_t *);
   1026 					if (ri->ri_hwbits) {
   1027 						*(u_char *)hp = clr;
   1028 						DELTA(hp, 1, int32_t *);
   1029 					}
   1030 				}
   1031 			}
   1032 		}
   1033 
   1034 		return;
   1035 	}
   1036 
   1037 	slop1 = (4 - ((long)rp & 3)) & 3;
   1038 	slop2 = (num - slop1) & 3;
   1039 	num -= slop1 + slop2;
   1040 	n8 = num >> 5;
   1041 	num = (num >> 2) & 7;
   1042 
   1043 	while (height--) {
   1044 		dp = rp;
   1045 		DELTA(rp, ri->ri_stride, int32_t *);
   1046 		if (ri->ri_hwbits) {
   1047 			hp = hrp;
   1048 			DELTA(hrp, ri->ri_stride, int32_t *);
   1049 		}
   1050 
   1051 		/* Align span to 4 bytes */
   1052 		if (slop1 & 1) {
   1053 			*(u_char *)dp = clr;
   1054 			DELTA(dp, 1, int32_t *);
   1055 			if (ri->ri_hwbits) {
   1056 				*(u_char *)hp = clr;
   1057 				DELTA(hp, 1, int32_t *);
   1058 			}
   1059 		}
   1060 
   1061 		if (slop1 & 2) {
   1062 			*(int16_t *)dp = clr;
   1063 			DELTA(dp, 2, int32_t *);
   1064 			if (ri->ri_hwbits) {
   1065 				*(int16_t *)hp = clr;
   1066 				DELTA(hp, 2, int32_t *);
   1067 			}
   1068 		}
   1069 
   1070 		/* Write 32 bytes per loop */
   1071 		for (cnt = n8; cnt; cnt--) {
   1072 			for (i = 0; i < 8; i++) {
   1073 				dp[i] = clr;
   1074 				if (ri->ri_hwbits)
   1075 					hp[i] = clr;
   1076 			}
   1077 			dp += 8;
   1078 			if (ri->ri_hwbits)
   1079 				hp += 8;
   1080 		}
   1081 
   1082 		/* Write 4 bytes per loop */
   1083 		for (cnt = num; cnt; cnt--) {
   1084 			*dp++ = clr;
   1085 			if (ri->ri_hwbits)
   1086 				*hp++ = clr;
   1087 		}
   1088 
   1089 		/* Write unaligned trailing slop */
   1090 		if (slop2 & 1) {
   1091 			*(u_char *)dp = clr;
   1092 			DELTA(dp, 1, int32_t *);
   1093 			if (ri->ri_hwbits) {
   1094 				*(u_char *)hp = clr;
   1095 				DELTA(hp, 1, int32_t *);
   1096 			}
   1097 		}
   1098 
   1099 		if (slop2 & 2) {
   1100 			*(int16_t *)dp = clr;
   1101 			if (ri->ri_hwbits)
   1102 				*(int16_t *)hp = clr;
   1103 		}
   1104 	}
   1105 }
   1106