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