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