Home | History | Annotate | Line # | Download | only in rasops
rasops.c revision 1.74
      1 /*	 $NetBSD: rasops.c,v 1.74 2017/02/23 12:16:30 nonaka 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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: rasops.c,v 1.74 2017/02/23 12:16:30 nonaka Exp $");
     34 
     35 #include "opt_rasops.h"
     36 #include "rasops_glue.h"
     37 #include "opt_wsmsgattrs.h"
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/time.h>
     42 #include <sys/kmem.h>
     43 
     44 #include <sys/bswap.h>
     45 #include <machine/endian.h>
     46 
     47 #include <dev/wscons/wsdisplayvar.h>
     48 #include <dev/wscons/wsconsio.h>
     49 #include <dev/wsfont/wsfont.h>
     50 #include <dev/rasops/rasops.h>
     51 
     52 #ifndef _KERNEL
     53 #include <errno.h>
     54 #endif
     55 
     56 #ifdef RASOPS_DEBUG
     57 #define DPRINTF aprint_error
     58 #else
     59 #define DPRINTF while (0) printf
     60 #endif
     61 
     62 struct rasops_matchdata {
     63 	struct rasops_info *ri;
     64 	int wantcols, wantrows;
     65 	int bestscore;
     66 	struct wsdisplay_font *pick;
     67 	int ident;
     68 };
     69 
     70 /* ANSI colormap (R,G,B). Upper 8 are high-intensity */
     71 const u_char rasops_cmap[256*3] = {
     72 	0x00, 0x00, 0x00, /* black */
     73 	0x7f, 0x00, 0x00, /* red */
     74 	0x00, 0x7f, 0x00, /* green */
     75 	0x7f, 0x7f, 0x00, /* brown */
     76 	0x00, 0x00, 0x7f, /* blue */
     77 	0x7f, 0x00, 0x7f, /* magenta */
     78 	0x00, 0x7f, 0x7f, /* cyan */
     79 	0xc7, 0xc7, 0xc7, /* white - XXX too dim? */
     80 
     81 	0x7f, 0x7f, 0x7f, /* black */
     82 	0xff, 0x00, 0x00, /* red */
     83 	0x00, 0xff, 0x00, /* green */
     84 	0xff, 0xff, 0x00, /* brown */
     85 	0x00, 0x00, 0xff, /* blue */
     86 	0xff, 0x00, 0xff, /* magenta */
     87 	0x00, 0xff, 0xff, /* cyan */
     88 	0xff, 0xff, 0xff, /* white */
     89 
     90 	/*
     91 	 * For the cursor, we need at least the last (255th)
     92 	 * color to be white. Fill up white completely for
     93 	 * simplicity.
     94 	 */
     95 #define _CMWHITE 0xff, 0xff, 0xff,
     96 #define _CMWHITE16	_CMWHITE _CMWHITE _CMWHITE _CMWHITE \
     97 			_CMWHITE _CMWHITE _CMWHITE _CMWHITE \
     98 			_CMWHITE _CMWHITE _CMWHITE _CMWHITE \
     99 			_CMWHITE _CMWHITE _CMWHITE _CMWHITE
    100 	_CMWHITE16 _CMWHITE16 _CMWHITE16 _CMWHITE16 _CMWHITE16
    101 	_CMWHITE16 _CMWHITE16 _CMWHITE16 _CMWHITE16 _CMWHITE16
    102 	_CMWHITE16 _CMWHITE16 _CMWHITE16 _CMWHITE16 /* but not the last one */
    103 #undef _CMWHITE16
    104 #undef _CMWHITE
    105 
    106 	/*
    107 	 * For the cursor the fg/bg indices are bit inverted, so
    108 	 * provide complimentary colors in the upper 16 entries.
    109 	 */
    110 	0x7f, 0x7f, 0x7f, /* black */
    111 	0xff, 0x00, 0x00, /* red */
    112 	0x00, 0xff, 0x00, /* green */
    113 	0xff, 0xff, 0x00, /* brown */
    114 	0x00, 0x00, 0xff, /* blue */
    115 	0xff, 0x00, 0xff, /* magenta */
    116 	0x00, 0xff, 0xff, /* cyan */
    117 	0xff, 0xff, 0xff, /* white */
    118 
    119 	0x00, 0x00, 0x00, /* black */
    120 	0x7f, 0x00, 0x00, /* red */
    121 	0x00, 0x7f, 0x00, /* green */
    122 	0x7f, 0x7f, 0x00, /* brown */
    123 	0x00, 0x00, 0x7f, /* blue */
    124 	0x7f, 0x00, 0x7f, /* magenta */
    125 	0x00, 0x7f, 0x7f, /* cyan */
    126 	0xc7, 0xc7, 0xc7, /* white - XXX too dim? */
    127 };
    128 
    129 /* True if color is gray */
    130 const u_char rasops_isgray[16] = {
    131 	1, 0, 0, 0,
    132 	0, 0, 0, 1,
    133 	1, 0, 0, 0,
    134 	0, 0, 0, 1
    135 };
    136 
    137 /* Generic functions */
    138 static void	rasops_copyrows(void *, int, int, int);
    139 static int	rasops_mapchar(void *, int, u_int *);
    140 static void	rasops_cursor(void *, int, int, int);
    141 static int	rasops_allocattr_color(void *, int, int, int, long *);
    142 static int	rasops_allocattr_mono(void *, int, int, int, long *);
    143 static void	rasops_do_cursor(struct rasops_info *);
    144 static void	rasops_init_devcmap(struct rasops_info *);
    145 
    146 #if NRASOPS_ROTATION > 0
    147 static void	rasops_rotate_font(int *, int);
    148 static void	rasops_copychar(void *, int, int, int, int);
    149 
    150 /* rotate clockwise */
    151 static void	rasops_copycols_rotated_cw(void *, int, int, int, int);
    152 static void	rasops_copyrows_rotated_cw(void *, int, int, int);
    153 static void	rasops_erasecols_rotated_cw(void *, int, int, int, long);
    154 static void	rasops_eraserows_rotated_cw(void *, int, int, long);
    155 static void	rasops_putchar_rotated_cw(void *, int, int, u_int, long);
    156 
    157 /* rotate counter-clockwise */
    158 static void	rasops_copychar_ccw(void *, int, int, int, int);
    159 static void	rasops_copycols_rotated_ccw(void *, int, int, int, int);
    160 static void	rasops_copyrows_rotated_ccw(void *, int, int, int);
    161 #define rasops_erasecols_rotated_ccw rasops_erasecols_rotated_cw
    162 #define rasops_eraserows_rotated_ccw rasops_eraserows_rotated_cw
    163 static void	rasops_putchar_rotated_ccw(void *, int, int, u_int, long);
    164 
    165 /*
    166  * List of all rotated fonts
    167  */
    168 SLIST_HEAD(, rotatedfont) rotatedfonts = SLIST_HEAD_INITIALIZER(rotatedfonts);
    169 struct rotatedfont {
    170 	SLIST_ENTRY(rotatedfont) rf_next;
    171 	int rf_cookie;
    172 	int rf_rotated;
    173 };
    174 #endif	/* NRASOPS_ROTATION > 0 */
    175 
    176 void	rasops_make_box_chars_8(struct rasops_info *);
    177 void	rasops_make_box_chars_16(struct rasops_info *);
    178 void	rasops_make_box_chars_32(struct rasops_info *);
    179 void	rasops_make_box_chars_alpha(struct rasops_info *);
    180 
    181 extern int cold;
    182 
    183 /*
    184  * Initialize a 'rasops_info' descriptor.
    185  */
    186 int
    187 rasops_init(struct rasops_info *ri, int wantrows, int wantcols)
    188 {
    189 
    190 	memset (&ri->ri_optfont, 0, sizeof(ri->ri_optfont));
    191 #ifdef _KERNEL
    192 	/* Select a font if the caller doesn't care */
    193 	if (ri->ri_font == NULL) {
    194 		int cookie = -1;
    195 		int flags;
    196 
    197 		wsfont_init();
    198 
    199 		/*
    200 		 * first, try to find something that's as close as possible
    201 		 * to the caller's requested terminal size
    202 		 */
    203 		if (wantrows == 0)
    204 			wantrows = RASOPS_DEFAULT_HEIGHT;
    205 		if (wantcols == 0)
    206 			wantcols = RASOPS_DEFAULT_WIDTH;
    207 
    208 		flags = WSFONT_FIND_BESTWIDTH | WSFONT_FIND_BITMAP;
    209 		if ((ri->ri_flg & RI_ENABLE_ALPHA) != 0)
    210 			flags |= WSFONT_FIND_ALPHA;
    211 
    212 		cookie = wsfont_find(NULL,
    213 			ri->ri_width / wantcols,
    214 			0,
    215 			0,
    216 			WSDISPLAY_FONTORDER_L2R,
    217 			WSDISPLAY_FONTORDER_L2R,
    218 			flags);
    219 
    220 		/*
    221 		 * this means there is no supported font in the list
    222 		 */
    223 		if (cookie <= 0) {
    224 			aprint_error("rasops_init: font table is empty\n");
    225 			return (-1);
    226 		}
    227 
    228 #if NRASOPS_ROTATION > 0
    229 		/*
    230 		 * Pick the rotated version of this font. This will create it
    231 		 * if necessary.
    232 		 */
    233 		if (ri->ri_flg & RI_ROTATE_MASK) {
    234 			if (ri->ri_flg & RI_ROTATE_CW)
    235 				rasops_rotate_font(&cookie, WSFONT_ROTATE_CW);
    236 			else if (ri->ri_flg & RI_ROTATE_CCW)
    237 				rasops_rotate_font(&cookie, WSFONT_ROTATE_CCW);
    238 		}
    239 #endif
    240 
    241 		if (wsfont_lock(cookie, &ri->ri_font)) {
    242 			aprint_error("rasops_init: couldn't lock font\n");
    243 			return (-1);
    244 		}
    245 
    246 		ri->ri_wsfcookie = cookie;
    247 	}
    248 #endif
    249 
    250 	/* This should never happen in reality... */
    251 #ifdef DEBUG
    252 	if ((long)ri->ri_bits & 3) {
    253 		aprint_error("rasops_init: bits not aligned on 32-bit boundary\n");
    254 		return (-1);
    255 	}
    256 
    257 	if ((int)ri->ri_stride & 3) {
    258 		aprint_error("rasops_init: stride not aligned on 32-bit boundary\n");
    259 		return (-1);
    260 	}
    261 #endif
    262 
    263 	if (rasops_reconfig(ri, wantrows, wantcols))
    264 		return (-1);
    265 
    266 	rasops_init_devcmap(ri);
    267 	return (0);
    268 }
    269 
    270 /*
    271  * Reconfigure (because parameters have changed in some way).
    272  */
    273 int
    274 rasops_reconfig(struct rasops_info *ri, int wantrows, int wantcols)
    275 {
    276 	int bpp, s, len;
    277 
    278 	s = splhigh();
    279 
    280 	if (wantrows == 0)
    281 		wantrows = RASOPS_DEFAULT_HEIGHT;
    282 	if (wantcols == 0)
    283 		wantcols = RASOPS_DEFAULT_WIDTH;
    284 
    285 	/* throw away old line drawing character bitmaps, if we have any */
    286 	if (ri->ri_optfont.data != NULL) {
    287 		kmem_free(ri->ri_optfont.data, ri->ri_optfont.stride *
    288 		    ri->ri_optfont.fontheight * ri->ri_optfont.numchars);
    289 		ri->ri_optfont.data = NULL;
    290 	}
    291 
    292 	/* autogenerate box drawing characters */
    293 	ri->ri_optfont.firstchar = WSFONT_FLAG_OPT;
    294 	ri->ri_optfont.numchars = 16;
    295 	ri->ri_optfont.fontwidth = ri->ri_font->fontwidth;
    296 	ri->ri_optfont.fontheight = ri->ri_font->fontheight;
    297 	ri->ri_optfont.stride = ri->ri_font->stride;
    298 	len = ri->ri_optfont.fontheight * ri->ri_optfont.stride *
    299 		      ri->ri_optfont.numchars;
    300 
    301 	if (((ri->ri_flg & RI_NO_AUTO) == 0) &&
    302 	  ((ri->ri_optfont.data = kmem_zalloc(len, KM_SLEEP)) != NULL)) {
    303 
    304 		if (ri->ri_optfont.stride < ri->ri_optfont.fontwidth) {
    305 			switch (ri->ri_optfont.stride) {
    306 			case 1:
    307 				rasops_make_box_chars_8(ri);
    308 				break;
    309 			case 2:
    310 				rasops_make_box_chars_16(ri);
    311 				break;
    312 			case 4:
    313 				rasops_make_box_chars_32(ri);
    314 				break;
    315 			}
    316 		} else {
    317 			rasops_make_box_chars_alpha(ri);
    318 		}
    319 	} else
    320 		memset(&ri->ri_optfont, 0, sizeof(ri->ri_optfont));
    321 
    322 	if (ri->ri_font->fontwidth > 32 || ri->ri_font->fontwidth < 4)
    323 		panic("rasops_init: fontwidth assumptions botched!");
    324 
    325 	/* Need this to frob the setup below */
    326 	bpp = (ri->ri_depth == 15 ? 16 : ri->ri_depth);
    327 
    328 	if ((ri->ri_flg & RI_CFGDONE) != 0) {
    329 		ri->ri_bits = ri->ri_origbits;
    330 		ri->ri_hwbits = ri->ri_hworigbits;
    331 	}
    332 
    333 	/* Don't care if the caller wants a hideously small console */
    334 	if (wantrows < 10)
    335 		wantrows = 10;
    336 
    337 	if (wantcols < 20)
    338 		wantcols = 20;
    339 
    340 	/* Now constrain what they get */
    341 	ri->ri_emuwidth = ri->ri_font->fontwidth * wantcols;
    342 	ri->ri_emuheight = ri->ri_font->fontheight * wantrows;
    343 
    344 	if (ri->ri_emuwidth > ri->ri_width)
    345 		ri->ri_emuwidth = ri->ri_width;
    346 
    347 	if (ri->ri_emuheight > ri->ri_height)
    348 		ri->ri_emuheight = ri->ri_height;
    349 
    350 	/* Reduce width until aligned on a 32-bit boundary */
    351 	while ((ri->ri_emuwidth * bpp & 31) != 0)
    352 		ri->ri_emuwidth--;
    353 
    354 #if NRASOPS_ROTATION > 0
    355 	if (ri->ri_flg & (RI_ROTATE_CW|RI_ROTATE_CCW)) {
    356 		ri->ri_rows = ri->ri_emuwidth / ri->ri_font->fontwidth;
    357 		ri->ri_cols = ri->ri_emuheight / ri->ri_font->fontheight;
    358 	} else
    359 #endif
    360 	{
    361 
    362 		ri->ri_cols = ri->ri_emuwidth / ri->ri_font->fontwidth;
    363 		ri->ri_rows = ri->ri_emuheight / ri->ri_font->fontheight;
    364 	}
    365 	ri->ri_emustride = ri->ri_emuwidth * bpp >> 3;
    366 	ri->ri_delta = ri->ri_stride - ri->ri_emustride;
    367 	ri->ri_ccol = 0;
    368 	ri->ri_crow = 0;
    369 	ri->ri_pelbytes = bpp >> 3;
    370 
    371 	ri->ri_xscale = (ri->ri_font->fontwidth * bpp) >> 3;
    372 	ri->ri_yscale = ri->ri_font->fontheight * ri->ri_stride;
    373 	ri->ri_fontscale = ri->ri_font->fontheight * ri->ri_font->stride;
    374 
    375 #ifdef DEBUG
    376 	if ((ri->ri_delta & 3) != 0)
    377 		panic("rasops_init: ri_delta not aligned on 32-bit boundary");
    378 #endif
    379 	/* Clear the entire display */
    380 	if ((ri->ri_flg & RI_CLEAR) != 0)
    381 		memset(ri->ri_bits, 0, ri->ri_stride * ri->ri_height);
    382 
    383 	/* Now centre our window if needs be */
    384 	ri->ri_origbits = ri->ri_bits;
    385 	ri->ri_hworigbits = ri->ri_hwbits;
    386 
    387 	if ((ri->ri_flg & RI_CENTER) != 0) {
    388 		ri->ri_bits += (((ri->ri_width * bpp >> 3) -
    389 		    ri->ri_emustride) >> 1) & ~3;
    390 		ri->ri_bits += ((ri->ri_height - ri->ri_emuheight) >> 1) *
    391 		    ri->ri_stride;
    392 		if (ri->ri_hwbits != NULL) {
    393 			ri->ri_hwbits += (((ri->ri_width * bpp >> 3) -
    394 			    ri->ri_emustride) >> 1) & ~3;
    395 			ri->ri_hwbits += ((ri->ri_height - ri->ri_emuheight) >> 1) *
    396 			    ri->ri_stride;
    397 		}
    398 		ri->ri_yorigin = (int)(ri->ri_bits - ri->ri_origbits)
    399 		   / ri->ri_stride;
    400 		ri->ri_xorigin = (((int)(ri->ri_bits - ri->ri_origbits)
    401 		   % ri->ri_stride) * 8 / bpp);
    402 	} else
    403 		ri->ri_xorigin = ri->ri_yorigin = 0;
    404 
    405 	/*
    406 	 * Fill in defaults for operations set.  XXX this nukes private
    407 	 * routines used by accelerated fb drivers.
    408 	 */
    409 	ri->ri_ops.mapchar = rasops_mapchar;
    410 	ri->ri_ops.copyrows = rasops_copyrows;
    411 	ri->ri_ops.copycols = rasops_copycols;
    412 	ri->ri_ops.erasecols = rasops_erasecols;
    413 	ri->ri_ops.eraserows = rasops_eraserows;
    414 	ri->ri_ops.cursor = rasops_cursor;
    415 	ri->ri_do_cursor = rasops_do_cursor;
    416 
    417 	if (ri->ri_depth < 8 || (ri->ri_flg & RI_FORCEMONO) != 0) {
    418 		ri->ri_ops.allocattr = rasops_allocattr_mono;
    419 		ri->ri_caps = WSSCREEN_UNDERLINE | WSSCREEN_REVERSE;
    420 	} else {
    421 		ri->ri_ops.allocattr = rasops_allocattr_color;
    422 		ri->ri_caps = WSSCREEN_UNDERLINE | WSSCREEN_HILIT |
    423 		    WSSCREEN_WSCOLORS | WSSCREEN_REVERSE;
    424 	}
    425 
    426 	switch (ri->ri_depth) {
    427 #if NRASOPS1 > 0
    428 	case 1:
    429 		rasops1_init(ri);
    430 		break;
    431 #endif
    432 #if NRASOPS2 > 0
    433 	case 2:
    434 		rasops2_init(ri);
    435 		break;
    436 #endif
    437 #if NRASOPS4 > 0
    438 	case 4:
    439 		rasops4_init(ri);
    440 		break;
    441 #endif
    442 #if NRASOPS8 > 0
    443 	case 8:
    444 		rasops8_init(ri);
    445 		break;
    446 #endif
    447 #if NRASOPS15 > 0 || NRASOPS16 > 0
    448 	case 15:
    449 	case 16:
    450 		rasops15_init(ri);
    451 		break;
    452 #endif
    453 #if NRASOPS24 > 0
    454 	case 24:
    455 		rasops24_init(ri);
    456 		break;
    457 #endif
    458 #if NRASOPS32 > 0
    459 	case 32:
    460 		rasops32_init(ri);
    461 		break;
    462 #endif
    463 	default:
    464 		ri->ri_flg &= ~RI_CFGDONE;
    465 		splx(s);
    466 		return (-1);
    467 	}
    468 
    469 #if NRASOPS_ROTATION > 0
    470 	if (ri->ri_flg & RI_ROTATE_MASK) {
    471 		if (ri->ri_flg & RI_ROTATE_CW) {
    472 			ri->ri_real_ops = ri->ri_ops;
    473 			ri->ri_ops.copycols = rasops_copycols_rotated_cw;
    474 			ri->ri_ops.copyrows = rasops_copyrows_rotated_cw;
    475 			ri->ri_ops.erasecols = rasops_erasecols_rotated_cw;
    476 			ri->ri_ops.eraserows = rasops_eraserows_rotated_cw;
    477 			ri->ri_ops.putchar = rasops_putchar_rotated_cw;
    478 		} else if (ri->ri_flg & RI_ROTATE_CCW) {
    479 			ri->ri_real_ops = ri->ri_ops;
    480 			ri->ri_ops.copycols = rasops_copycols_rotated_ccw;
    481 			ri->ri_ops.copyrows = rasops_copyrows_rotated_ccw;
    482 			ri->ri_ops.erasecols = rasops_erasecols_rotated_ccw;
    483 			ri->ri_ops.eraserows = rasops_eraserows_rotated_ccw;
    484 			ri->ri_ops.putchar = rasops_putchar_rotated_ccw;
    485 		}
    486 	}
    487 #endif
    488 
    489 	ri->ri_flg |= RI_CFGDONE;
    490 	splx(s);
    491 	return (0);
    492 }
    493 
    494 /*
    495  * Map a character.
    496  */
    497 static int
    498 rasops_mapchar(void *cookie, int c, u_int *cp)
    499 {
    500 	struct rasops_info *ri;
    501 
    502 	ri = (struct rasops_info *)cookie;
    503 
    504 #ifdef DIAGNOSTIC
    505 	if (ri->ri_font == NULL)
    506 		panic("rasops_mapchar: no font selected");
    507 #endif
    508 
    509 	if ( (c = wsfont_map_unichar(ri->ri_font, c)) < 0) {
    510 		*cp = ' ';
    511 		return (0);
    512 	}
    513 
    514 	if (c < ri->ri_font->firstchar) {
    515 		*cp = ' ';
    516 		return (0);
    517 	}
    518 
    519 #if 0
    520 	if (c - ri->ri_font->firstchar >= ri->ri_font->numchars) {
    521 		*cp = ' ';
    522 		return (0);
    523 	}
    524 #endif
    525 	*cp = c;
    526 	return (5);
    527 }
    528 
    529 /*
    530  * Allocate a color attribute.
    531  */
    532 static int
    533 rasops_allocattr_color(void *cookie, int fg, int bg, int flg,
    534     long *attr)
    535 {
    536 	int swap;
    537 
    538 	if (__predict_false((unsigned int)fg >= sizeof(rasops_isgray) ||
    539 	    (unsigned int)bg >= sizeof(rasops_isgray)))
    540 		return (EINVAL);
    541 
    542 #ifdef RASOPS_CLIPPING
    543 	fg &= 7;
    544 	bg &= 7;
    545 #endif
    546 	if ((flg & WSATTR_BLINK) != 0)
    547 		return (EINVAL);
    548 
    549 	if ((flg & WSATTR_WSCOLORS) == 0) {
    550 #ifdef WS_DEFAULT_FG
    551 		fg = WS_DEFAULT_FG;
    552 #else
    553 		fg = WSCOL_WHITE;
    554 #endif
    555 #ifdef WS_DEFAULT_BG
    556 		bg = WS_DEFAULT_BG;
    557 #else
    558 		bg = WSCOL_BLACK;
    559 #endif
    560 	}
    561 
    562 	if ((flg & WSATTR_REVERSE) != 0) {
    563 		swap = fg;
    564 		fg = bg;
    565 		bg = swap;
    566 	}
    567 
    568 	if ((flg & WSATTR_HILIT) != 0)
    569 		fg += 8;
    570 
    571 	flg = ((flg & WSATTR_UNDERLINE) ? 1 : 0);
    572 
    573 	if (rasops_isgray[fg])
    574 		flg |= 2;
    575 
    576 	if (rasops_isgray[bg])
    577 		flg |= 4;
    578 
    579 	*attr = (bg << 16) | (fg << 24) | flg;
    580 	return (0);
    581 }
    582 
    583 /*
    584  * Allocate a mono attribute.
    585  */
    586 static int
    587 rasops_allocattr_mono(void *cookie, int fg, int bg, int flg,
    588     long *attr)
    589 {
    590 	int swap;
    591 
    592 	if ((flg & (WSATTR_BLINK | WSATTR_HILIT | WSATTR_WSCOLORS)) != 0)
    593 		return (EINVAL);
    594 
    595 	fg = 1;
    596 	bg = 0;
    597 
    598 	if ((flg & WSATTR_REVERSE) != 0) {
    599 		swap = fg;
    600 		fg = bg;
    601 		bg = swap;
    602 	}
    603 
    604 	*attr = (bg << 16) | (fg << 24) | ((flg & WSATTR_UNDERLINE) ? 7 : 6);
    605 	return (0);
    606 }
    607 
    608 /*
    609  * Copy rows.
    610  */
    611 static void
    612 rasops_copyrows(void *cookie, int src, int dst, int num)
    613 {
    614 	int32_t *sp, *dp, *hp, *srp, *drp, *hrp;
    615 	struct rasops_info *ri;
    616 	int n8, n1, cnt, delta;
    617 
    618 	ri = (struct rasops_info *)cookie;
    619 	hp = hrp = NULL;
    620 
    621 #ifdef RASOPS_CLIPPING
    622 	if (dst == src)
    623 		return;
    624 
    625 	if (src < 0) {
    626 		num += src;
    627 		src = 0;
    628 	}
    629 
    630 	if ((src + num) > ri->ri_rows)
    631 		num = ri->ri_rows - src;
    632 
    633 	if (dst < 0) {
    634 		num += dst;
    635 		dst = 0;
    636 	}
    637 
    638 	if ((dst + num) > ri->ri_rows)
    639 		num = ri->ri_rows - dst;
    640 
    641 	if (num <= 0)
    642 		return;
    643 #endif
    644 
    645 	num *= ri->ri_font->fontheight;
    646 	n8 = ri->ri_emustride >> 5;
    647 	n1 = (ri->ri_emustride >> 2) & 7;
    648 
    649 	if (dst < src) {
    650 		srp = (int32_t *)(ri->ri_bits + src * ri->ri_yscale);
    651 		drp = (int32_t *)(ri->ri_bits + dst * ri->ri_yscale);
    652 		if (ri->ri_hwbits)
    653 			hrp = (int32_t *)(ri->ri_hwbits + dst *
    654 			    ri->ri_yscale);
    655 		delta = ri->ri_stride;
    656 	} else {
    657 		src = ri->ri_font->fontheight * src + num - 1;
    658 		dst = ri->ri_font->fontheight * dst + num - 1;
    659 		srp = (int32_t *)(ri->ri_bits + src * ri->ri_stride);
    660 		drp = (int32_t *)(ri->ri_bits + dst * ri->ri_stride);
    661 		if (ri->ri_hwbits)
    662 			hrp = (int32_t *)(ri->ri_hwbits + dst *
    663 			    ri->ri_stride);
    664 
    665 		delta = -ri->ri_stride;
    666 	}
    667 
    668 	while (num--) {
    669 		dp = drp;
    670 		sp = srp;
    671 		if (ri->ri_hwbits)
    672 			hp = hrp;
    673 
    674 		DELTA(drp, delta, int32_t *);
    675 		DELTA(srp, delta, int32_t *);
    676 		if (ri->ri_hwbits)
    677 			DELTA(hrp, delta, int32_t *);
    678 
    679 		for (cnt = n8; cnt; cnt--) {
    680 			dp[0] = sp[0];
    681 			dp[1] = sp[1];
    682 			dp[2] = sp[2];
    683 			dp[3] = sp[3];
    684 			dp[4] = sp[4];
    685 			dp[5] = sp[5];
    686 			dp[6] = sp[6];
    687 			dp[7] = sp[7];
    688 			dp += 8;
    689 			sp += 8;
    690 		}
    691 		if (ri->ri_hwbits) {
    692 			sp -= (8 * n8);
    693 			for (cnt = n8; cnt; cnt--) {
    694 				hp[0] = sp[0];
    695 				hp[1] = sp[1];
    696 				hp[2] = sp[2];
    697 				hp[3] = sp[3];
    698 				hp[4] = sp[4];
    699 				hp[5] = sp[5];
    700 				hp[6] = sp[6];
    701 				hp[7] = sp[7];
    702 				hp += 8;
    703 				sp += 8;
    704 			}
    705 		}
    706 
    707 		for (cnt = n1; cnt; cnt--) {
    708 			*dp++ = *sp++;
    709 			if (ri->ri_hwbits)
    710 				*hp++ = *(sp - 1);
    711 		}
    712 	}
    713 }
    714 
    715 /*
    716  * Copy columns. This is slow, and hard to optimize due to alignment,
    717  * and the fact that we have to copy both left->right and right->left.
    718  * We simply cop-out here and use memmove(), since it handles all of
    719  * these cases anyway.
    720  */
    721 void
    722 rasops_copycols(void *cookie, int row, int src, int dst, int num)
    723 {
    724 	struct rasops_info *ri;
    725 	u_char *sp, *dp, *hp;
    726 	int height;
    727 
    728 	ri = (struct rasops_info *)cookie;
    729 	hp = NULL;
    730 
    731 #ifdef RASOPS_CLIPPING
    732 	if (dst == src)
    733 		return;
    734 
    735 	/* Catches < 0 case too */
    736 	if ((unsigned)row >= (unsigned)ri->ri_rows)
    737 		return;
    738 
    739 	if (src < 0) {
    740 		num += src;
    741 		src = 0;
    742 	}
    743 
    744 	if ((src + num) > ri->ri_cols)
    745 		num = ri->ri_cols - src;
    746 
    747 	if (dst < 0) {
    748 		num += dst;
    749 		dst = 0;
    750 	}
    751 
    752 	if ((dst + num) > ri->ri_cols)
    753 		num = ri->ri_cols - dst;
    754 
    755 	if (num <= 0)
    756 		return;
    757 #endif
    758 
    759 	num *= ri->ri_xscale;
    760 	row *= ri->ri_yscale;
    761 	height = ri->ri_font->fontheight;
    762 
    763 	sp = ri->ri_bits + row + src * ri->ri_xscale;
    764 	dp = ri->ri_bits + row + dst * ri->ri_xscale;
    765 	if (ri->ri_hwbits)
    766 		hp = ri->ri_hwbits + row + dst * ri->ri_xscale;
    767 
    768 	while (height--) {
    769 		memmove(dp, sp, num);
    770 		if (ri->ri_hwbits) {
    771 			memcpy(hp, sp, num);
    772 			hp += ri->ri_stride;
    773 		}
    774 		dp += ri->ri_stride;
    775 		sp += ri->ri_stride;
    776 	}
    777 }
    778 
    779 /*
    780  * Turn cursor off/on.
    781  */
    782 static void
    783 rasops_cursor(void *cookie, int on, int row, int col)
    784 {
    785 	struct rasops_info *ri;
    786 
    787 	ri = (struct rasops_info *)cookie;
    788 
    789 	/* Turn old cursor off */
    790 	if ((ri->ri_flg & RI_CURSOR) != 0)
    791 #ifdef RASOPS_CLIPPING
    792 		if ((ri->ri_flg & RI_CURSORCLIP) == 0)
    793 #endif
    794 			ri->ri_do_cursor(ri);
    795 
    796 	/* Select new cursor */
    797 #ifdef RASOPS_CLIPPING
    798 	ri->ri_flg &= ~RI_CURSORCLIP;
    799 
    800 	if (row < 0 || row >= ri->ri_rows)
    801 		ri->ri_flg |= RI_CURSORCLIP;
    802 	else if (col < 0 || col >= ri->ri_cols)
    803 		ri->ri_flg |= RI_CURSORCLIP;
    804 #endif
    805 	ri->ri_crow = row;
    806 	ri->ri_ccol = col;
    807 
    808 	if (on) {
    809 		ri->ri_flg |= RI_CURSOR;
    810 #ifdef RASOPS_CLIPPING
    811 		if ((ri->ri_flg & RI_CURSORCLIP) == 0)
    812 #endif
    813 			ri->ri_do_cursor(ri);
    814 	} else
    815 		ri->ri_flg &= ~RI_CURSOR;
    816 }
    817 
    818 /*
    819  * Make the device colormap
    820  */
    821 static void
    822 rasops_init_devcmap(struct rasops_info *ri)
    823 {
    824 	const u_char *p;
    825 	int i, c;
    826 
    827 	switch (ri->ri_depth) {
    828 	case 1:
    829 		ri->ri_devcmap[0] = 0;
    830 		for (i = 1; i < 16; i++)
    831 			ri->ri_devcmap[i] = -1;
    832 		return;
    833 
    834 	case 2:
    835 		for (i = 1; i < 15; i++)
    836 			ri->ri_devcmap[i] = 0xaaaaaaaa;
    837 
    838 		ri->ri_devcmap[0] = 0;
    839 		ri->ri_devcmap[8] = 0x55555555;
    840 		ri->ri_devcmap[15] = -1;
    841 		return;
    842 
    843 	case 8:
    844 		if ((ri->ri_flg & RI_8BIT_IS_RGB) == 0) {
    845 			for (i = 0; i < 16; i++)
    846 				ri->ri_devcmap[i] =
    847 				    i | (i<<8) | (i<<16) | (i<<24);
    848 			return;
    849 		}
    850 	}
    851 
    852 	p = rasops_cmap;
    853 
    854 	for (i = 0; i < 16; i++) {
    855 		if (ri->ri_rnum <= 8)
    856 			c = (*p >> (8 - ri->ri_rnum)) << ri->ri_rpos;
    857 		else
    858 			c = (*p << (ri->ri_rnum - 8)) << ri->ri_rpos;
    859 		p++;
    860 
    861 		if (ri->ri_gnum <= 8)
    862 			c |= (*p >> (8 - ri->ri_gnum)) << ri->ri_gpos;
    863 		else
    864 			c |= (*p << (ri->ri_gnum - 8)) << ri->ri_gpos;
    865 		p++;
    866 
    867 		if (ri->ri_bnum <= 8)
    868 			c |= (*p >> (8 - ri->ri_bnum)) << ri->ri_bpos;
    869 		else
    870 			c |= (*p << (ri->ri_bnum - 8)) << ri->ri_bpos;
    871 		p++;
    872 
    873 		/* Fill the word for generic routines, which want this */
    874 		if (ri->ri_depth == 24)
    875 			c = c | ((c & 0xff) << 24);
    876 		else if (ri->ri_depth == 8) {
    877 			c = c | (c << 8);
    878 			c |= c << 16;
    879 		} else if (ri->ri_depth <= 16)
    880 			c = c | (c << 16);
    881 
    882 		/* 24bpp does bswap on the fly. {32,16,15}bpp do it here. */
    883 		if ((ri->ri_flg & RI_BSWAP) == 0)
    884 			ri->ri_devcmap[i] = c;
    885 		else if (ri->ri_depth == 32)
    886 			ri->ri_devcmap[i] = bswap32(c);
    887 		else if (ri->ri_depth == 16 || ri->ri_depth == 15)
    888 			ri->ri_devcmap[i] = bswap16(c);
    889 		else
    890 			ri->ri_devcmap[i] = c;
    891 	}
    892 }
    893 
    894 /*
    895  * Unpack a rasops attribute
    896  */
    897 void
    898 rasops_unpack_attr(long attr, int *fg, int *bg, int *underline)
    899 {
    900 
    901 	*fg = ((u_int)attr >> 24) & 0xf;
    902 	*bg = ((u_int)attr >> 16) & 0xf;
    903 	if (underline != NULL)
    904 		*underline = (u_int)attr & 1;
    905 }
    906 
    907 /*
    908  * Erase rows. This isn't static, since 24-bpp uses it in special cases.
    909  */
    910 void
    911 rasops_eraserows(void *cookie, int row, int num, long attr)
    912 {
    913 	struct rasops_info *ri;
    914 	int np, nw, cnt, delta;
    915 	int32_t *dp, *hp, clr;
    916 	int i;
    917 
    918 	ri = (struct rasops_info *)cookie;
    919 	hp = NULL;
    920 
    921 #ifdef RASOPS_CLIPPING
    922 	if (row < 0) {
    923 		num += row;
    924 		row = 0;
    925 	}
    926 
    927 	if ((row + num) > ri->ri_rows)
    928 		num = ri->ri_rows - row;
    929 
    930 	if (num <= 0)
    931 		return;
    932 #endif
    933 
    934 	clr = ri->ri_devcmap[(attr >> 16) & 0xf];
    935 
    936 	/*
    937 	 * XXX The wsdisplay_emulops interface seems a little deficient in
    938 	 * that there is no way to clear the *entire* screen. We provide a
    939 	 * workaround here: if the entire console area is being cleared, and
    940 	 * the RI_FULLCLEAR flag is set, clear the entire display.
    941 	 */
    942 	if (num == ri->ri_rows && (ri->ri_flg & RI_FULLCLEAR) != 0) {
    943 		np = ri->ri_stride >> 5;
    944 		nw = (ri->ri_stride >> 2) & 7;
    945 		num = ri->ri_height;
    946 		dp = (int32_t *)ri->ri_origbits;
    947 		if (ri->ri_hwbits)
    948 			hp = (int32_t *)ri->ri_hworigbits;
    949 		delta = 0;
    950 	} else {
    951 		np = ri->ri_emustride >> 5;
    952 		nw = (ri->ri_emustride >> 2) & 7;
    953 		num *= ri->ri_font->fontheight;
    954 		dp = (int32_t *)(ri->ri_bits + row * ri->ri_yscale);
    955 		if (ri->ri_hwbits)
    956 			hp = (int32_t *)(ri->ri_hwbits + row *
    957 			    ri->ri_yscale);
    958 		delta = ri->ri_delta;
    959 	}
    960 
    961 	while (num--) {
    962 		for (cnt = np; cnt; cnt--) {
    963 			for (i = 0; i < 8; i++) {
    964 				dp[i] = clr;
    965 				if (ri->ri_hwbits)
    966 					hp[i] = clr;
    967 			}
    968 			dp += 8;
    969 			if (ri->ri_hwbits)
    970 				hp += 8;
    971 		}
    972 
    973 		for (cnt = nw; cnt; cnt--) {
    974 			*(int32_t *)dp = clr;
    975 			DELTA(dp, 4, int32_t *);
    976 			if (ri->ri_hwbits) {
    977 				*(int32_t *)hp = clr;
    978 				DELTA(hp, 4, int32_t *);
    979 			}
    980 		}
    981 
    982 		DELTA(dp, delta, int32_t *);
    983 		if (ri->ri_hwbits)
    984 			DELTA(hp, delta, int32_t *);
    985 	}
    986 }
    987 
    988 /*
    989  * Actually turn the cursor on or off. This does the dirty work for
    990  * rasops_cursor().
    991  */
    992 static void
    993 rasops_do_cursor(struct rasops_info *ri)
    994 {
    995 	int full1, height, cnt, slop1, slop2, row, col;
    996 	u_char *dp, *rp, *hrp, *hp, tmp = 0;
    997 
    998 	hrp = hp = NULL;
    999 
   1000 #if NRASOPS_ROTATION > 0
   1001 	if (ri->ri_flg & RI_ROTATE_MASK) {
   1002 		if (ri->ri_flg & RI_ROTATE_CW) {
   1003 			/* Rotate rows/columns */
   1004 			row = ri->ri_ccol;
   1005 			col = ri->ri_rows - ri->ri_crow - 1;
   1006 		} else if (ri->ri_flg & RI_ROTATE_CCW) {
   1007 			/* Rotate rows/columns */
   1008 			row = ri->ri_cols - ri->ri_ccol - 1;
   1009 			col = ri->ri_crow;
   1010 		} else {	/* upside-down */
   1011 			row = ri->ri_crow;
   1012 			col = ri->ri_ccol;
   1013 		}
   1014 	} else
   1015 #endif
   1016 	{
   1017 		row = ri->ri_crow;
   1018 		col = ri->ri_ccol;
   1019 	}
   1020 
   1021 	rp = ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale;
   1022 	if (ri->ri_hwbits)
   1023 		hrp = ri->ri_hwbits + row * ri->ri_yscale + col
   1024 		    * ri->ri_xscale;
   1025 	height = ri->ri_font->fontheight;
   1026 	slop1 = (4 - ((long)rp & 3)) & 3;
   1027 
   1028 	if (slop1 > ri->ri_xscale)
   1029 		slop1 = ri->ri_xscale;
   1030 
   1031 	slop2 = (ri->ri_xscale - slop1) & 3;
   1032 	full1 = (ri->ri_xscale - slop1 - slop2) >> 2;
   1033 
   1034 	if ((slop1 | slop2) == 0) {
   1035 		uint32_t tmp32;
   1036 		/* A common case */
   1037 		while (height--) {
   1038 			dp = rp;
   1039 			rp += ri->ri_stride;
   1040 			if (ri->ri_hwbits) {
   1041 				hp = hrp;
   1042 				hrp += ri->ri_stride;
   1043 			}
   1044 
   1045 			for (cnt = full1; cnt; cnt--) {
   1046 				tmp32 = *(int32_t *)dp ^ ~0;
   1047 				*(int32_t *)dp = tmp32;
   1048 				dp += 4;
   1049 				if (ri->ri_hwbits) {
   1050 					*(int32_t *)hp = tmp32;
   1051 					hp += 4;
   1052 				}
   1053 			}
   1054 		}
   1055 	} else {
   1056 		uint16_t tmp16;
   1057 		uint32_t tmp32;
   1058 		/* XXX this is stupid.. use masks instead */
   1059 		while (height--) {
   1060 			dp = rp;
   1061 			rp += ri->ri_stride;
   1062 			if (ri->ri_hwbits) {
   1063 				hp = hrp;
   1064 				hrp += ri->ri_stride;
   1065 			}
   1066 
   1067 			if (slop1 & 1) {
   1068 				tmp = *dp ^ ~0;
   1069 				*dp = tmp;
   1070 				dp++;
   1071 				if (ri->ri_hwbits) {
   1072 					*hp++ = tmp;
   1073 				}
   1074 			}
   1075 
   1076 			if (slop1 & 2) {
   1077 				tmp16 = *(int16_t *)dp ^ ~0;
   1078 				*(uint16_t *)dp = tmp16;
   1079 				dp += 2;
   1080 				if (ri->ri_hwbits) {
   1081 					*(int16_t *)hp = tmp16;
   1082 					hp += 2;
   1083 				}
   1084 			}
   1085 
   1086 			for (cnt = full1; cnt; cnt--) {
   1087 				tmp32 = *(int32_t *)dp ^ ~0;
   1088 				*(uint32_t *)dp = tmp32;
   1089 				dp += 4;
   1090 				if (ri->ri_hwbits) {
   1091 					*(int32_t *)hp = tmp32;
   1092 					hp += 4;
   1093 				}
   1094 			}
   1095 
   1096 			if (slop2 & 1) {
   1097 				tmp = *dp ^ ~0;
   1098 				*dp = tmp;
   1099 				dp++;
   1100 				if (ri->ri_hwbits)
   1101 					*hp++ = tmp;
   1102 			}
   1103 
   1104 			if (slop2 & 2) {
   1105 				tmp16 = *(int16_t *)dp ^ ~0;
   1106 				*(uint16_t *)dp = tmp16;
   1107 				if (ri->ri_hwbits)
   1108 					*(int16_t *)hp = tmp16;
   1109 			}
   1110 		}
   1111 	}
   1112 }
   1113 
   1114 /*
   1115  * Erase columns.
   1116  */
   1117 void
   1118 rasops_erasecols(void *cookie, int row, int col, int num, long attr)
   1119 {
   1120 	int n8, height, cnt, slop1, slop2, clr;
   1121 	struct rasops_info *ri;
   1122 	int32_t *rp, *dp, *hrp, *hp;
   1123 	int i;
   1124 
   1125 	ri = (struct rasops_info *)cookie;
   1126 	hrp = hp = NULL;
   1127 
   1128 #ifdef RASOPS_CLIPPING
   1129 	if ((unsigned)row >= (unsigned)ri->ri_rows)
   1130 		return;
   1131 
   1132 	if (col < 0) {
   1133 		num += col;
   1134 		col = 0;
   1135 	}
   1136 
   1137 	if ((col + num) > ri->ri_cols)
   1138 		num = ri->ri_cols - col;
   1139 
   1140 	if (num <= 0)
   1141 		return;
   1142 #endif
   1143 
   1144 	num = num * ri->ri_xscale;
   1145 	rp = (int32_t *)(ri->ri_bits + row*ri->ri_yscale + col*ri->ri_xscale);
   1146 	if (ri->ri_hwbits)
   1147 		hrp = (int32_t *)(ri->ri_hwbits + row*ri->ri_yscale +
   1148 		    col*ri->ri_xscale);
   1149 	height = ri->ri_font->fontheight;
   1150 	clr = ri->ri_devcmap[(attr >> 16) & 0xf];
   1151 
   1152 	/* Don't bother using the full loop for <= 32 pels */
   1153 	if (num <= 32) {
   1154 		if (((num | ri->ri_xscale) & 3) == 0) {
   1155 			/* Word aligned blt */
   1156 			num >>= 2;
   1157 
   1158 			while (height--) {
   1159 				dp = rp;
   1160 				DELTA(rp, ri->ri_stride, int32_t *);
   1161 				if (ri->ri_hwbits) {
   1162 					hp = hrp;
   1163 					DELTA(hrp, ri->ri_stride, int32_t *);
   1164 				}
   1165 
   1166 				for (cnt = num; cnt; cnt--) {
   1167 					*dp++ = clr;
   1168 					if (ri->ri_hwbits)
   1169 						*hp++ = clr;
   1170 				}
   1171 			}
   1172 		} else if (((num | ri->ri_xscale) & 1) == 0) {
   1173 			/*
   1174 			 * Halfword aligned blt. This is needed so the
   1175 			 * 15/16 bit ops can use this function.
   1176 			 */
   1177 			num >>= 1;
   1178 
   1179 			while (height--) {
   1180 				dp = rp;
   1181 				DELTA(rp, ri->ri_stride, int32_t *);
   1182 				if (ri->ri_hwbits) {
   1183 					hp = hrp;
   1184 					DELTA(hrp, ri->ri_stride, int32_t *);
   1185 				}
   1186 
   1187 				for (cnt = num; cnt; cnt--) {
   1188 					*(int16_t *)dp = clr;
   1189 					DELTA(dp, 2, int32_t *);
   1190 					if (ri->ri_hwbits) {
   1191 						*(int16_t *)hp = clr;
   1192 						DELTA(hp, 2, int32_t *);
   1193 					}
   1194 				}
   1195 			}
   1196 		} else {
   1197 			while (height--) {
   1198 				dp = rp;
   1199 				DELTA(rp, ri->ri_stride, int32_t *);
   1200 				if (ri->ri_hwbits) {
   1201 					hp = hrp;
   1202 					DELTA(hrp, ri->ri_stride, int32_t *);
   1203 				}
   1204 
   1205 				for (cnt = num; cnt; cnt--) {
   1206 					*(u_char *)dp = clr;
   1207 					DELTA(dp, 1, int32_t *);
   1208 					if (ri->ri_hwbits) {
   1209 						*(u_char *)hp = clr;
   1210 						DELTA(hp, 1, int32_t *);
   1211 					}
   1212 				}
   1213 			}
   1214 		}
   1215 
   1216 		return;
   1217 	}
   1218 
   1219 	slop1 = (4 - ((long)rp & 3)) & 3;
   1220 	slop2 = (num - slop1) & 3;
   1221 	num -= slop1 + slop2;
   1222 	n8 = num >> 5;
   1223 	num = (num >> 2) & 7;
   1224 
   1225 	while (height--) {
   1226 		dp = rp;
   1227 		DELTA(rp, ri->ri_stride, int32_t *);
   1228 		if (ri->ri_hwbits) {
   1229 			hp = hrp;
   1230 			DELTA(hrp, ri->ri_stride, int32_t *);
   1231 		}
   1232 
   1233 		/* Align span to 4 bytes */
   1234 		if (slop1 & 1) {
   1235 			*(u_char *)dp = clr;
   1236 			DELTA(dp, 1, int32_t *);
   1237 			if (ri->ri_hwbits) {
   1238 				*(u_char *)hp = clr;
   1239 				DELTA(hp, 1, int32_t *);
   1240 			}
   1241 		}
   1242 
   1243 		if (slop1 & 2) {
   1244 			*(int16_t *)dp = clr;
   1245 			DELTA(dp, 2, int32_t *);
   1246 			if (ri->ri_hwbits) {
   1247 				*(int16_t *)hp = clr;
   1248 				DELTA(hp, 2, int32_t *);
   1249 			}
   1250 		}
   1251 
   1252 		/* Write 32 bytes per loop */
   1253 		for (cnt = n8; cnt; cnt--) {
   1254 			for (i = 0; i < 8; i++) {
   1255 				dp[i] = clr;
   1256 				if (ri->ri_hwbits)
   1257 					hp[i] = clr;
   1258 			}
   1259 			dp += 8;
   1260 			if (ri->ri_hwbits)
   1261 				hp += 8;
   1262 		}
   1263 
   1264 		/* Write 4 bytes per loop */
   1265 		for (cnt = num; cnt; cnt--) {
   1266 			*dp++ = clr;
   1267 			if (ri->ri_hwbits)
   1268 				*hp++ = clr;
   1269 		}
   1270 
   1271 		/* Write unaligned trailing slop */
   1272 		if (slop2 & 1) {
   1273 			*(u_char *)dp = clr;
   1274 			DELTA(dp, 1, int32_t *);
   1275 			if (ri->ri_hwbits) {
   1276 				*(u_char *)hp = clr;
   1277 				DELTA(hp, 1, int32_t *);
   1278 			}
   1279 		}
   1280 
   1281 		if (slop2 & 2) {
   1282 			*(int16_t *)dp = clr;
   1283 			if (ri->ri_hwbits)
   1284 				*(int16_t *)hp = clr;
   1285 		}
   1286 	}
   1287 }
   1288 
   1289 #if NRASOPS_ROTATION > 0
   1290 /*
   1291  * Quarter clockwise rotation routines (originally intended for the
   1292  * built-in Zaurus C3x00 display in 16bpp).
   1293  */
   1294 
   1295 #include <sys/malloc.h>
   1296 
   1297 static void
   1298 rasops_rotate_font(int *cookie, int rotate)
   1299 {
   1300 	struct rotatedfont *f;
   1301 	int ncookie;
   1302 
   1303 	SLIST_FOREACH(f, &rotatedfonts, rf_next) {
   1304 		if (f->rf_cookie == *cookie) {
   1305 			*cookie = f->rf_rotated;
   1306 			return;
   1307 		}
   1308 	}
   1309 
   1310 	/*
   1311 	 * We did not find a rotated version of this font. Ask the wsfont
   1312 	 * code to compute one for us.
   1313 	 */
   1314 
   1315 	f = malloc(sizeof(struct rotatedfont), M_DEVBUF, M_WAITOK);
   1316 	if (f == NULL)
   1317 		goto fail0;
   1318 
   1319 	if ((ncookie = wsfont_rotate(*cookie, rotate)) == -1)
   1320 		goto fail1;
   1321 
   1322 	f->rf_cookie = *cookie;
   1323 	f->rf_rotated = ncookie;
   1324 	SLIST_INSERT_HEAD(&rotatedfonts, f, rf_next);
   1325 
   1326 	*cookie = ncookie;
   1327 	return;
   1328 
   1329 fail1:	free(f, M_DEVBUF);
   1330 fail0:	/* Just use the existing font, I guess...  */
   1331 	return;
   1332 }
   1333 
   1334 static void
   1335 rasops_copychar(void *cookie, int srcrow, int dstrow, int srccol, int dstcol)
   1336 {
   1337 	struct rasops_info *ri;
   1338 	u_char *sp, *dp;
   1339 	int height;
   1340 	int r_srcrow, r_dstrow, r_srccol, r_dstcol;
   1341 
   1342 	ri = (struct rasops_info *)cookie;
   1343 
   1344 	r_srcrow = srccol;
   1345 	r_dstrow = dstcol;
   1346 	r_srccol = ri->ri_rows - srcrow - 1;
   1347 	r_dstcol = ri->ri_rows - dstrow - 1;
   1348 
   1349 	r_srcrow *= ri->ri_yscale;
   1350 	r_dstrow *= ri->ri_yscale;
   1351 	height = ri->ri_font->fontheight;
   1352 
   1353 	sp = ri->ri_bits + r_srcrow + r_srccol * ri->ri_xscale;
   1354 	dp = ri->ri_bits + r_dstrow + r_dstcol * ri->ri_xscale;
   1355 
   1356 	while (height--) {
   1357 		memmove(dp, sp, ri->ri_xscale);
   1358 		dp += ri->ri_stride;
   1359 		sp += ri->ri_stride;
   1360 	}
   1361 }
   1362 
   1363 static void
   1364 rasops_putchar_rotated_cw(void *cookie, int row, int col, u_int uc, long attr)
   1365 {
   1366 	struct rasops_info *ri;
   1367 	u_char *rp;
   1368 	int height;
   1369 
   1370 	ri = (struct rasops_info *)cookie;
   1371 
   1372 	if (__predict_false((unsigned int)row > ri->ri_rows ||
   1373 	    (unsigned int)col > ri->ri_cols))
   1374 		return;
   1375 
   1376 	/* Avoid underflow */
   1377 	if ((ri->ri_rows - row - 1) < 0)
   1378 		return;
   1379 
   1380 	/* Do rotated char sans (side)underline */
   1381 	ri->ri_real_ops.putchar(cookie, col, ri->ri_rows - row - 1, uc,
   1382 	    attr & ~1);
   1383 
   1384 	/* Do rotated underline */
   1385 	rp = ri->ri_bits + col * ri->ri_yscale + (ri->ri_rows - row - 1) *
   1386 	    ri->ri_xscale;
   1387 	height = ri->ri_font->fontheight;
   1388 
   1389 	/* XXX this assumes 16-bit color depth */
   1390 	if ((attr & 1) != 0) {
   1391 		int16_t c = (int16_t)ri->ri_devcmap[((u_int)attr >> 24) & 0xf];
   1392 
   1393 		while (height--) {
   1394 			*(int16_t *)rp = c;
   1395 			rp += ri->ri_stride;
   1396 		}
   1397 	}
   1398 }
   1399 
   1400 static void
   1401 rasops_erasecols_rotated_cw(void *cookie, int row, int col, int num, long attr)
   1402 {
   1403 	struct rasops_info *ri;
   1404 	int i;
   1405 
   1406 	ri = (struct rasops_info *)cookie;
   1407 
   1408 	for (i = col; i < col + num; i++)
   1409 		ri->ri_ops.putchar(cookie, row, i, ' ', attr);
   1410 }
   1411 
   1412 /* XXX: these could likely be optimised somewhat. */
   1413 static void
   1414 rasops_copyrows_rotated_cw(void *cookie, int src, int dst, int num)
   1415 {
   1416 	struct rasops_info *ri = (struct rasops_info *)cookie;
   1417 	int col, roff;
   1418 
   1419 	if (src > dst)
   1420 		for (roff = 0; roff < num; roff++)
   1421 			for (col = 0; col < ri->ri_cols; col++)
   1422 				rasops_copychar(cookie, src + roff, dst + roff,
   1423 				    col, col);
   1424 	else
   1425 		for (roff = num - 1; roff >= 0; roff--)
   1426 			for (col = 0; col < ri->ri_cols; col++)
   1427 				rasops_copychar(cookie, src + roff, dst + roff,
   1428 				    col, col);
   1429 }
   1430 
   1431 static void
   1432 rasops_copycols_rotated_cw(void *cookie, int row, int src, int dst, int num)
   1433 {
   1434 	int coff;
   1435 
   1436 	if (src > dst)
   1437 		for (coff = 0; coff < num; coff++)
   1438 			rasops_copychar(cookie, row, row, src + coff, dst + coff);
   1439 	else
   1440 		for (coff = num - 1; coff >= 0; coff--)
   1441 			rasops_copychar(cookie, row, row, src + coff, dst + coff);
   1442 }
   1443 
   1444 static void
   1445 rasops_eraserows_rotated_cw(void *cookie, int row, int num, long attr)
   1446 {
   1447 	struct rasops_info *ri;
   1448 	int col, rn;
   1449 
   1450 	ri = (struct rasops_info *)cookie;
   1451 
   1452 	for (rn = row; rn < row + num; rn++)
   1453 		for (col = 0; col < ri->ri_cols; col++)
   1454 			ri->ri_ops.putchar(cookie, rn, col, ' ', attr);
   1455 }
   1456 
   1457 /*
   1458  * Quarter counter-clockwise rotation routines (originally intended for the
   1459  * built-in Sharp W-ZERO3 display in 16bpp).
   1460  */
   1461 static void
   1462 rasops_copychar_ccw(void *cookie, int srcrow, int dstrow, int srccol, int dstcol)
   1463 {
   1464 	struct rasops_info *ri;
   1465 	u_char *sp, *dp;
   1466 	int height;
   1467 	int r_srcrow, r_dstrow, r_srccol, r_dstcol;
   1468 
   1469 	ri = (struct rasops_info *)cookie;
   1470 
   1471 	r_srcrow = ri->ri_cols - srccol - 1;
   1472 	r_dstrow = ri->ri_cols - dstcol - 1;
   1473 	r_srccol = srcrow;
   1474 	r_dstcol = dstrow;
   1475 
   1476 	r_srcrow *= ri->ri_yscale;
   1477 	r_dstrow *= ri->ri_yscale;
   1478 	height = ri->ri_font->fontheight;
   1479 
   1480 	sp = ri->ri_bits + r_srcrow + r_srccol * ri->ri_xscale;
   1481 	dp = ri->ri_bits + r_dstrow + r_dstcol * ri->ri_xscale;
   1482 
   1483 	while (height--) {
   1484 		memmove(dp, sp, ri->ri_xscale);
   1485 		dp += ri->ri_stride;
   1486 		sp += ri->ri_stride;
   1487 	}
   1488 }
   1489 
   1490 static void
   1491 rasops_putchar_rotated_ccw(void *cookie, int row, int col, u_int uc, long attr)
   1492 {
   1493 	struct rasops_info *ri;
   1494 	u_char *rp;
   1495 	int height;
   1496 
   1497 	ri = (struct rasops_info *)cookie;
   1498 
   1499 	if (__predict_false((unsigned int)row > ri->ri_rows ||
   1500 	    (unsigned int)col > ri->ri_cols))
   1501 		return;
   1502 
   1503 	/* Avoid underflow */
   1504 	if ((ri->ri_cols - col - 1) < 0)
   1505 		return;
   1506 
   1507 	/* Do rotated char sans (side)underline */
   1508 	ri->ri_real_ops.putchar(cookie, ri->ri_cols - col - 1, row, uc,
   1509 	    attr & ~1);
   1510 
   1511 	/* Do rotated underline */
   1512 	rp = ri->ri_bits + (ri->ri_cols - col - 1) * ri->ri_yscale +
   1513 	    row * ri->ri_xscale +
   1514 	    (ri->ri_font->fontwidth - 1) * ri->ri_pelbytes;
   1515 	height = ri->ri_font->fontheight;
   1516 
   1517 	/* XXX this assumes 16-bit color depth */
   1518 	if ((attr & 1) != 0) {
   1519 		int16_t c = (int16_t)ri->ri_devcmap[((u_int)attr >> 24) & 0xf];
   1520 
   1521 		while (height--) {
   1522 			*(int16_t *)rp = c;
   1523 			rp += ri->ri_stride;
   1524 		}
   1525 	}
   1526 }
   1527 
   1528 /* XXX: these could likely be optimised somewhat. */
   1529 static void
   1530 rasops_copyrows_rotated_ccw(void *cookie, int src, int dst, int num)
   1531 {
   1532 	struct rasops_info *ri = (struct rasops_info *)cookie;
   1533 	int col, roff;
   1534 
   1535 	if (src > dst)
   1536 		for (roff = 0; roff < num; roff++)
   1537 			for (col = 0; col < ri->ri_cols; col++)
   1538 				rasops_copychar_ccw(cookie,
   1539 				    src + roff, dst + roff, col, col);
   1540 	else
   1541 		for (roff = num - 1; roff >= 0; roff--)
   1542 			for (col = 0; col < ri->ri_cols; col++)
   1543 				rasops_copychar_ccw(cookie,
   1544 				    src + roff, dst + roff, col, col);
   1545 }
   1546 
   1547 static void
   1548 rasops_copycols_rotated_ccw(void *cookie, int row, int src, int dst, int num)
   1549 {
   1550 	int coff;
   1551 
   1552 	if (src > dst)
   1553 		for (coff = 0; coff < num; coff++)
   1554 			rasops_copychar_ccw(cookie, row, row,
   1555 			    src + coff, dst + coff);
   1556 	else
   1557 		for (coff = num - 1; coff >= 0; coff--)
   1558 			rasops_copychar_ccw(cookie, row, row,
   1559 			    src + coff, dst + coff);
   1560 }
   1561 #endif	/* NRASOPS_ROTATION */
   1562 
   1563 void
   1564 rasops_make_box_chars_16(struct rasops_info *ri)
   1565 {
   1566 	uint16_t vert_mask, hmask_left, hmask_right;
   1567 	uint16_t *data = (uint16_t *)ri->ri_optfont.data;
   1568 	int c, i, mid;
   1569 
   1570 	vert_mask = 0xc000 >> ((ri->ri_font->fontwidth >> 1) - 1);
   1571 	hmask_left = 0xff00 << (8 - (ri->ri_font->fontwidth >> 1));
   1572 	hmask_right = hmask_left >> ((ri->ri_font->fontwidth + 1)>> 1);
   1573 	mid = (ri->ri_font->fontheight + 1) >> 1;
   1574 
   1575 	/* 0x00 would be empty anyway so don't bother */
   1576 	for (c = 1; c < 16; c++) {
   1577 		data += ri->ri_font->fontheight;
   1578 		if (c & 1) {
   1579 			/* upper segment */
   1580 			for (i = 0; i < mid; i++)
   1581 				data[i] = vert_mask;
   1582 		}
   1583 		if (c & 4) {
   1584 			/* lower segment */
   1585 			for (i = mid; i < ri->ri_font->fontheight; i++)
   1586 				data[i] = vert_mask;
   1587 		}
   1588 		if (c & 2) {
   1589 			/* right segment */
   1590 			i = ri->ri_font->fontheight >> 1;
   1591 			data[mid - 1] |= hmask_right;
   1592 			data[mid] |= hmask_right;
   1593 		}
   1594 		if (c & 8) {
   1595 			/* left segment */
   1596 			data[mid - 1] |= hmask_left;
   1597 			data[mid] |= hmask_left;
   1598 		}
   1599 	}
   1600 }
   1601 
   1602 void
   1603 rasops_make_box_chars_8(struct rasops_info *ri)
   1604 {
   1605 	uint8_t vert_mask, hmask_left, hmask_right;
   1606 	uint8_t *data = (uint8_t *)ri->ri_optfont.data;
   1607 	int c, i, mid;
   1608 
   1609 	vert_mask = 0xc0 >> ((ri->ri_font->fontwidth >> 1) - 1);
   1610 	hmask_left = 0xf0 << (4 - (ri->ri_font->fontwidth >> 1));
   1611 	hmask_right = hmask_left >> ((ri->ri_font->fontwidth + 1)>> 1);
   1612 	mid = (ri->ri_font->fontheight + 1) >> 1;
   1613 
   1614 	/* 0x00 would be empty anyway so don't bother */
   1615 	for (c = 1; c < 16; c++) {
   1616 		data += ri->ri_font->fontheight;
   1617 		if (c & 1) {
   1618 			/* upper segment */
   1619 			for (i = 0; i < mid; i++)
   1620 				data[i] = vert_mask;
   1621 		}
   1622 		if (c & 4) {
   1623 			/* lower segment */
   1624 			for (i = mid; i < ri->ri_font->fontheight; i++)
   1625 				data[i] = vert_mask;
   1626 		}
   1627 		if (c & 2) {
   1628 			/* right segment */
   1629 			i = ri->ri_font->fontheight >> 1;
   1630 			data[mid - 1] |= hmask_right;
   1631 			data[mid] |= hmask_right;
   1632 		}
   1633 		if (c & 8) {
   1634 			/* left segment */
   1635 			data[mid - 1] |= hmask_left;
   1636 			data[mid] |= hmask_left;
   1637 		}
   1638 	}
   1639 }
   1640 
   1641 void
   1642 rasops_make_box_chars_32(struct rasops_info *ri)
   1643 {
   1644 	uint32_t vert_mask, hmask_left, hmask_right;
   1645 	uint32_t *data = (uint32_t *)ri->ri_optfont.data;
   1646 	int c, i, mid;
   1647 
   1648 	vert_mask = 0xc0000000 >> ((ri->ri_font->fontwidth >> 1) - 1);
   1649 	hmask_left = 0xffff0000 << (16 - (ri->ri_font->fontwidth >> 1));
   1650 	hmask_right = hmask_left >> ((ri->ri_font->fontwidth + 1)>> 1);
   1651 	mid = (ri->ri_font->fontheight + 1) >> 1;
   1652 
   1653 	/* 0x00 would be empty anyway so don't bother */
   1654 	for (c = 1; c < 16; c++) {
   1655 		data += ri->ri_font->fontheight;
   1656 		if (c & 1) {
   1657 			/* upper segment */
   1658 			for (i = 0; i < mid; i++)
   1659 				data[i] = vert_mask;
   1660 		}
   1661 		if (c & 4) {
   1662 			/* lower segment */
   1663 			for (i = mid; i < ri->ri_font->fontheight; i++)
   1664 				data[i] = vert_mask;
   1665 		}
   1666 		if (c & 2) {
   1667 			/* right segment */
   1668 			i = ri->ri_font->fontheight >> 1;
   1669 			data[mid - 1] |= hmask_right;
   1670 			data[mid] |= hmask_right;
   1671 		}
   1672 		if (c & 8) {
   1673 			/* left segment */
   1674 			data[mid - 1] |= hmask_left;
   1675 			data[mid] |= hmask_left;
   1676 		}
   1677 	}
   1678 }
   1679 
   1680 void
   1681 rasops_make_box_chars_alpha(struct rasops_info *ri)
   1682 {
   1683 	uint8_t *data = (uint8_t *)ri->ri_optfont.data;
   1684 	uint8_t *ddata;
   1685 	int c, i, hmid, vmid, wi, he;
   1686 
   1687 	wi = ri->ri_font->fontwidth;
   1688 	he = ri->ri_font->fontheight;
   1689 
   1690 	vmid = (he + 1) >> 1;
   1691 	hmid = (wi + 1) >> 1;
   1692 
   1693 	/* 0x00 would be empty anyway so don't bother */
   1694 	for (c = 1; c < 16; c++) {
   1695 		data += ri->ri_fontscale;
   1696 		if (c & 1) {
   1697 			/* upper segment */
   1698 			ddata = data + hmid;
   1699 			for (i = 0; i <= vmid; i++) {
   1700 				*ddata = 0xff;
   1701 				ddata += wi;
   1702 			}
   1703 		}
   1704 		if (c & 4) {
   1705 			/* lower segment */
   1706 			ddata = data + wi * vmid + hmid;
   1707 			for (i = vmid; i < he; i++) {
   1708 				*ddata = 0xff;
   1709 				ddata += wi;
   1710 			}
   1711 		}
   1712 		if (c & 2) {
   1713 			/* right segment */
   1714 			ddata = data + wi * vmid + hmid;
   1715 			for (i = hmid; i < wi; i++) {
   1716 				*ddata = 0xff;
   1717 				ddata++;
   1718 			}
   1719 		}
   1720 		if (c & 8) {
   1721 			/* left segment */
   1722 			ddata = data + wi * vmid;
   1723 			for (i = 0; i <= hmid; i++) {
   1724 				*ddata = 0xff;
   1725 				ddata++;
   1726 			}
   1727 		}
   1728 	}
   1729 }
   1730 
   1731 /*
   1732  * Return a colour map appropriate for the given struct rasops_info in the
   1733  * same form used by rasops_cmap[]
   1734  * For now this is either a copy of rasops_cmap[] or an R3G3B2 map, it should
   1735  * probably be a linear ( or gamma corrected? ) ramp for higher depths.
   1736  */
   1737 
   1738 int
   1739 rasops_get_cmap(struct rasops_info *ri, uint8_t *palette, size_t bytes)
   1740 {
   1741 	if ((ri->ri_depth == 8 ) && ((ri->ri_flg & RI_8BIT_IS_RGB) > 0)) {
   1742 		/* generate an R3G3B2 palette */
   1743 		int i, idx = 0;
   1744 		uint8_t tmp;
   1745 
   1746 		if (bytes < 768)
   1747 			return EINVAL;
   1748 		for (i = 0; i < 256; i++) {
   1749 			tmp = i & 0xe0;
   1750 			/*
   1751 			 * replicate bits so 0xe0 maps to a red value of 0xff
   1752 			 * in order to make white look actually white
   1753 			 */
   1754 			tmp |= (tmp >> 3) | (tmp >> 6);
   1755 			palette[idx] = tmp;
   1756 			idx++;
   1757 
   1758 			tmp = (i & 0x1c) << 3;
   1759 			tmp |= (tmp >> 3) | (tmp >> 6);
   1760 			palette[idx] = tmp;
   1761 			idx++;
   1762 
   1763 			tmp = (i & 0x03) << 6;
   1764 			tmp |= tmp >> 2;
   1765 			tmp |= tmp >> 4;
   1766 			palette[idx] = tmp;
   1767 			idx++;
   1768 		}
   1769 	} else {
   1770 		memcpy(palette, rasops_cmap, MIN(bytes, sizeof(rasops_cmap)));
   1771 	}
   1772 	return 0;
   1773 }
   1774