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