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